0% found this document useful (0 votes)
4 views2 pages

Data Cleaning in R: A Beginner's Guide

Uploaded by

henriquezmd
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views2 pages

Data Cleaning in R: A Beginner's Guide

Uploaded by

henriquezmd
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Data cleaning in R

By: Rodrigo Henriquez, ITM 2024

First steps
Always remember to “set your working directory”. In RStudio you can do it following the menu: ‘Session’ ->‘Set Working Directory’ -
> ‘Choose Directory…’ Or use the shortcut ‘Ctrl’+‘Shift’+‘H’ and select the folder desired as working directory.

If you need to restart your R session in RStudio you can use the shortcut ‘Ctrl’+‘Shift’+‘F10’.

To clear objects and loaded packages to clear your global work space you can use: rm(list = ls())

Import data
There are several ways to import data in RStudio. Some data sets are already included in R libraries. In the following example we will
use the ‘Melanoma’ dataset included in the ‘MASS’ package:

library(MASS) # load the 'MASS' package


data("Melanoma")

You can see the names of the variables included in the dataset using the function Names:

names(Melanoma)

## [1] "time" "status" "sex" "age" "year" "thickness"


## [7] "ulcer"

To preview the first elements of the dataset we can use the head function:

head(Melanoma) # print the first elements of the data frame

time status sex age year thickness ulcer


<int> <int> <int> <int> <int> <dbl> <int>

1 10 3 1 76 1972 6.76 1

2 30 3 1 56 1968 0.65 0

3 35 2 1 41 1977 1.34 0

4 99 3 0 71 1968 2.90 0

5 185 1 1 52 1965 12.08 1

6 204 1 1 28 1971 4.84 1

6 rows

Instead, you can also see the last elements using the tail function:

tail(Melanoma) # print the last elements of the data frame

time status sex age year thickness ulcer


<int> <int> <int> <int> <int> <dbl> <int>

200 4479 2 0 19 1965 1.13 1

201 4492 2 1 29 1965 7.06 1

202 4668 2 0 40 1965 6.12 0

203 4688 2 0 42 1965 0.48 0

204 4926 2 0 50 1964 2.26 0


time status sex age year thickness ulcer
<int> <int> <int> <int> <int> <dbl> <int>

205 5565 2 0 41 1962 2.90 0

6 rows

To open a spreadsheet-style viewer of your data we can use the View function:

View(Melanoma) # see a spreadsheet-style data viewer

We can perform operations over the data, like calculate the summary statistics for a numerical variable. In this example we write:
function_name(dataframe_name$variable_name)

summary(Melanoma$age) # summary statistics for a numerical variable

## Min. 1st Qu. Median Mean 3rd Qu. Max.


## 4.00 42.00 54.00 52.46 65.00 95.00

To avoid writing the dataframe name everytime we want to perform an operation on a variable, we can attach the dataframe in R.
This is very useful for small to medium datasets, but be careful with big datasets.

attach(Melanoma) # access variables without writing the dataset name

Now we can repeat the calculation of the summary statistics using only the name of the variable(s):

summary(thickness) # summary statistics for the 'thickness' variable

## Min. 1st Qu. Median Mean 3rd Qu. Max.


## 0.10 0.97 1.94 2.92 3.56 17.42

For categorical variables, is best to specify R to treat them as factors with the [Link] function, before performing the analysis:

summary([Link](sex)) # count of observations per category

## 0 1
## 126 79

Import a dataset in .csv or .xlsl formats


The easiest way is to follow the Menu: ‘File’ -> ‘Import Dataset’ -> ‘From Text (base)…’ and select the path to the location of your
.csv file

This will open the Import Dataset dialog box. Make sure the options are appropriate for your dataset.

mydata <- [Link](“C:/Rworkspace/datasets/[Link]”, header = TRUE)

[Link] for commas as separators and periods for decimals read.csv2 for semicolons as separators and commas for decimals

To import an Excel file, follow the menu ‘File’ -> ‘Import Dataset’ -> ‘From Excel’

In the ‘Import Dataset’ dialog box you can choose the name of your imported dataframe, and the ‘Sheet’ to import, among other
options.

Common questions

Powered by AI

Using the `summary` function on a numerical dataset provides key statistics like minimum, quartiles, median, mean, and maximum values, which are essential for understanding the data's baseline distribution and central tendency. This step is crucial as it informs decisions on further data manipulation, handling outliers, normalization, and choosing appropriate statistical methods for analysis .

In the 'Melanoma' dataset, the `sex` variable, when treated as a factor using `as.factor`, distinguishes between categories 0 and 1, which represent female and male, respectively. This categorical breakdown allows counting and summarizing the dataset in terms of gender distribution, facilitating gender-based analysis .

To import a dataset in R, you can use the `read.csv` function for CSV files, specifying the correct path and setting the `header` parameter to `TRUE`. For Excel files, RStudio provides an option through the menu ‘File’ -> ‘Import Dataset’ -> ‘From Excel’, where you can select the sheet and other import parameters. Different formats affect the delimiter and decimal conventions; `read.csv` uses commas for separators and periods for decimals, whereas `read.csv2` uses semicolons for separators and commas for decimals .

The `View` function in RStudio opens a spreadsheet-style data viewer, allowing interactive exploration of the dataset. This facilitates data exploration by enabling users to visually inspect data, navigate through entries, and quickly identify data patterns, anomalies, and relationships among variables, which is particularly useful for initial data exploration and cleaning .

Summary statistics for numerical variables are calculated using the `summary` function, which provides a concise statistical overview including minimum, 1st quartile, median, mean, 3rd quartile, and maximum values. These statistics are important as they offer a snapshot of the data distribution, help identify outliers, and inform subsequent statistical analyses by providing context for data variability and central tendency .

You can set the working directory in RStudio by navigating to the menu 'Session' -> 'Set Working Directory' -> 'Choose Directory…' or using the shortcut 'Ctrl'+'Shift'+'H' to select the desired folder. Setting the working directory is important because it defines the default location where R will read and write files, thereby facilitating organized data management and ensuring code reproducibility .

The `attach` function allows easy access to dataframe variables without repeatedly specifying the dataframe name, which simplifies code and enhances readability for small datasets. However, it can also lead to potential problems, such as conflicts with objects in the workspace or unintended consequences when working with large datasets due to memory considerations and global environment changes .

To preview data in RStudio, use the `head` function to view first elements, `tail` for last elements, or the `View` function for a spreadsheet-like data table. This is necessary to quickly inspect the data structure, verify data importation, and assess data summaries, ensuring that the dataset is as expected before proceeding with further analysis .

Summarizing categorical variables as factors in R with `as.factor` is advantageous because it treats them as discrete entities, enabling accurate representation and summary in analyses. This allows for clear distinction between categories and appropriate model fitting. Potential pitfalls include inadvertently treating continuous data as categorical or misclassifying data, leading to incorrect analysis conclusions .

Clearing the workspace in RStudio using `rm(list = ls())` removes all objects from the current environment. This is important to prevent unintended interactions between new and old objects, ensures reproducibility by removing lingering variables, and frees up memory, thus leading to more efficient use of computing resources .

You might also like