# Load necessary libraries
library(tidyverse) # for data manipulation and visualization
library(ggplot2) # specifically for ggplot (included in tidyverse but mentioned for clarity)
# Load your data
load_data <- function(file_path) {
tryCatch(
expr = {
data <- read.csv(file_path, stringsAsFactors = FALSE)
return(data)
},
error = function(e) {
print(paste("Error loading data:", e$message))
return(NULL)
}
)
}
# Clean and prepare the data
clean_data <- function(raw_data) {
if(is.null(raw_data)) return(NULL)
# Handle missing values (example: replacing with mean for numeric, mode for factor)
cleaned_data <- raw_data %>%
mutate_if(is.numeric, ~ ifelse(is.na(.), mean(., na.rm = TRUE), .)) %>%
mutate_if(is.character, ~ ifelse(is.na(.), "Unknown", .))
# Encode categorical variables if necessary (assuming 'category' is your categorical column)
# cleaned_data$category <- as.factor(cleaned_data$category)
return(cleaned_data)
}
# Perform QA steps
perform_qa <- function(cleaned_data) {
if(is.null(cleaned_data)) return()
# Check for remaining missing values
print("Remaining Missing Values:")
print(sapply(cleaned_data, function(x) sum(is.na(x))))
# Check data types
print("Data Types:")
print(sapply(cleaned_data, class))
# Summary statistics for numeric columns
numeric_cols <- cleaned_data %>% select(where(is.numeric)) %>% names()
if(length(numeric_cols) > 0) {
print("Summary Statistics for Numeric Columns:")
print(summary(cleaned_data[, numeric_cols]))
}
# Frequency counts for categorical columns
categorical_cols <- cleaned_data %>% select(where(is.character)) %>% names()
if(length(categorical_cols) > 0) {
print("Frequency Counts for Categorical Columns:")
for(col in categorical_cols) {
print(paste("Column:", col))
print(table(cleaned_data[, col]))
}
}
}
# Output plots for analysis
output_plots <- function(cleaned_data) {
if(is.null(cleaned_data)) return()
# Identify numeric and categorical columns dynamically
numeric_cols <- cleaned_data %>% select(where(is.numeric)) %>% names()
categorical_cols <- cleaned_data %>% select(where(is.character)) %>% names()
# Histograms for numeric variables
if(length(numeric_cols) > 0) {
for(col in numeric_cols) {
p <- ggplot(cleaned_data, aes_string(x = col)) +
geom_histogram(bins = 30, color = "black", fill = "lightblue") +
labs(title = paste("Distribution of", col), x = col, y = "Frequency")
print(p)
}
}
# Bar plots for categorical variables
if(length(categorical_cols) > 0) {
for(col in categorical_cols) {
cleaned_data %>%
count(.data[[col]]) %>%
ggplot(aes(x = .data[[col]], y = n)) +
geom_bar(stat = "identity", fill = "lightgreen") +
labs(title = paste("Distribution of", col), x = col, y = "Count")
}
}
# Scatter plots for pairs of numeric variables
if(length(numeric_cols) >= 2) {
for(i in 1:(length(numeric_cols) - 1)) {
for(j in (i + 1):length(numeric_cols)) {
col1 <- numeric_cols[i]
col2 <- numeric_cols[j]
p <- ggplot(cleaned_data, aes_string(x = col1, y = col2)) +
geom_point() +
labs(title = paste("Relationship Between", col1, "and", col2), x = col1, y = col2)
print(p)
}
}
}
}
# Main execution
main <- function() {
file_path <- "your_data.csv" # Update this path to match your file location
raw_data <- load_data(file_path)
cleaned_data <- clean_data(raw_data)
perform_qa(cleaned_data)
output_plots(cleaned_data)
}
# Run the main function
main()