R Programming Language Detailed Explanation Example of Data Wrangling and Cleaning.

Ahmet Taşdemir
4 min readDec 29, 2022

--

R is a programming language and software environment for statistical computing and graphics. It is widely used among statisticians and data scientists for developing statistical software and data analysis.

Some key features of R include:

  • A large collection of built-in functions and libraries for statistical analysis, data manipulation, and visualization.
  • A powerful and flexible programming language with a syntax similar to that of the S language, which was developed at Bell Labs in the 1970s.
  • An interactive environment that allows users to enter commands and see the results of their code immediately.
  • Support for a wide range of file formats and data sources, including CSV, Excel, SQL databases, and more.
  • A strong community of users and developers who contribute packages, documentation, and support to the R ecosystem.

R is often used for tasks such as:

  • Data wrangling and cleaning: R provides a variety of functions and packages for handling missing values, dealing with outliers, and reshaping and merging data.
  • Exploratory data analysis: R has a rich set of visualization and summary statistics tools for exploring and understanding data patterns and relationships.
  • Statistical modeling: R has a wide range of functions and packages for fitting and evaluating statistical models, including linear regression, logistic regression, and more.
  • Machine learning: R has several packages for implementing machine learning algorithms, such as decision trees, random forests, and support vector machines.
  • Data visualization: R has a variety of powerful packages for creating high-quality graphics and plots, including ggplot2, lattice, and more.

Simple R Project Code Example

Data wrangling and cleaning:

Here is a example of a program that demonstrates some common data wrangling and cleaning tasks in R:

# Load the dplyr and tidyr libraries
library(dplyr)
library(tidyr)

# Read in a CSV file
data <- read.csv("data.csv")

# Select specific columns from the data frame
selected_columns <- select(data, col1, col2, col3)

# Filter the data frame to only include rows where col1 is greater than 5
filtered_data <- filter(selected_columns, col1 > 5)

# Replace missing values in col2 with the mean value of col2
imputed_data <- mutate(filtered_data, col2 = ifelse(is.na(col2), mean(col2, na.rm = TRUE), col2))

# Group the data by col2 and calculate the mean of col3 for each group
grouped_data <- group_by(imputed_data, col2) %>% summarize(mean_col3 = mean(col3))

# Pivot the data so that each unique value of col2 is a separate column
pivoted_data <- pivot_wider(grouped_data, names_from = col2, values_from = mean_col3)

# Write the results to a new CSV file
write.csv(pivoted_data, "wrangled_data.csv")

This program reads in a CSV file, selects specific columns, filters the data to include only certain rows, imputes missing values, groups the data by one of the columns and calculates a summary statistic for each group, pivots the data to create new columns for each unique value of the grouping variable, and finally writes the resulting data to a new CSV file. These are just a few examples of the types of data wrangling and cleaning tasks that can be performed in R.

Here is a detailed explanation of the program I provided:

# Load the dplyr and tidyr libraries
library(dplyr)
library(tidyr)

In this line, we are loading the dplyr and tidyr libraries. These are R packages that provide a variety of functions for data manipulation and transformation.

# Read in a CSV file
data <- read.csv("data.csv")

This line reads in a CSV file called “data.csv” and stores the data in a data frame called data.

# Select specific columns from the data frame
selected_columns <- select(data, col1, col2, col3)

This line uses the select() function from the dplyr library to select specific columns from the data data frame and store the resulting data in a new data frame called selected_columns. In this case, we are selecting the columns "col1", "col2", and "col3".

# Filter the data frame to only include rows where col1 is greater than 5
filtered_data <- filter(selected_columns, col1 > 5)

This line uses the filter() function from the dplyr library to filter the selected_columns data frame to only include rows where the value in the "col1" column is greater than 5. The resulting data is stored in a new data frame called filtered_data.

# Replace missing values in col2 with the mean value of col2
imputed_data <- mutate(filtered_data, col2 = ifelse(is.na(col2), mean(col2, na.rm = TRUE), col2))

This line uses the mutate() function from the dplyr library to create a new column called "col2" in the filtered_data data frame. This new column is created by replacing missing values in the original "col2" column with the mean value of "col2", calculated using the mean() function with the na.rm argument set to TRUE to exclude missing values. The resulting data is stored in a new data frame called imputed_data.

# Group the data by col2 and calculate the mean of col3 for each group
grouped_data <- group_by(imputed_data, col2) %>% summarize(mean_col3 = mean(col3))

This line uses the group_by() and summarize() functions from the dplyr library to group the imputed_data data frame by the "col2" column and calculate the mean of the "col3" column for each group. The resulting data is stored in a new data frame called grouped_data.

# Pivot the data so that each unique value of col2 is a separate column
pivoted_data <- pivot_wider(grouped_data, names_from = col2, values_from = mean_col3)

This line uses the pivot_wider() function from the tidyr library to pivot the grouped_data data frame so that each unique value of the "col2" column becomes a separate column in

--

--