Data Science using R
Week 9 LAQ Answer: "Identify R function to Data Cleaning Process."
Here’s a detailed answer:
R Functions Used in the Data Cleaning Process
Data cleaning is an essential step in any data analysis workflow to ensure high-quality data for
modeling and insights. In R, several built-in and package functions help with typical cleaning tasks:
● Handling Missing Values
○ [Link]() — Identifies missing (NA) values in a dataset.
○ [Link]() — Removes rows containing any NA values.
○ replace() — Replaces NA values with specific values.
○ impute() (from the Hmisc or mice package) — Handles advanced imputation
methods for missing data.
● Detecting and Removing Duplicates
○ duplicated() — Flags duplicated rows.
○ unique() — Returns dataset with duplicate rows removed.
● Outlier Detection and Removal
○ boxplot() — Visualizes data to identify outliers.
○ subset() — Can be used to filter out outlier values based on thresholds.
● String Cleaning and Formatting
○ gsub() / sub() — Used for replacing patterns in strings (e.g., removing special
characters or whitespaces).
○ tolower() / toupper() — Converts strings to lower or upper case for consistency.
○ trimws() — Removes leading and trailing whitespace.
● Data Type Conversion
○ [Link]() / [Link]() / [Link]() — Changes column data types
as needed.
● Renaming Columns
○ names() — Used to rename dataset columns.
● General Data Manipulation (using Packages)
○ Functions from the dplyr package (filter(), mutate(), arrange(),
select(), rename()) — Powerful and efficient for cleaning and manipulating data
frames.
Example:
r
library(dplyr)
# Remove missing values
cleaned_data <- [Link](raw_data)
# Replace missing values in 'age' column with median
raw_data$age[[Link](raw_data$age)] <- median(raw_data$age, [Link] = TRUE)
# Remove duplicates
cleaned_data <- raw_data %>% distinct()
# String cleaning
cleaned_data$name <- tolower(trimws(gsub("[^A-Za-z ]", "", raw_data$name)))
Summary:
The most commonly used R functions for data cleaning are [Link](), [Link](), replace(),
duplicated(), unique(), gsub(), trimws(), and functions from dplyr such as filter(),
mutate(), and distinct() to streamline the process efficiently.