Can you provide me with an R script that loads data, cleans it, performs QA steps and…
- User
Can you provide me with an R script that loads data, cleans it, performs QA steps and outputs a few plots for analysis
- Assistant
Sure, I'd be happy to help you with that! Here's an example R script that loads a CSV file, cleans the data, performs some quality assurance (QA) steps, and outputs a few plots for analysis. In this example, I'll assume that the data is stored in a CSV file named "data.csv" and has a header row. You can modify the script to fit your specific data and requirements.
R# Load required libraries library(tidyverse) # for data manipulation and visualization library(lubridate) # for working with dates library(assertr) # for data validation # Function to load and clean data load_and_clean_data <- function(file_path) { # Load data from CSV file data <- read_csv(file_path) # Clean data data_cleaned <- data %>% # Convert date columns to date format mutate(date = ymd(date)) %>% # Remove rows with missing values in key columns filter(!is.na(date), !is.na(value)) %>% # Ensure that value column is numeric mutate(value = as.numeric(value)) return(data_cleaned) } # Function to perform QA steps perform_qa <- function(data) { # Check that all dates are within a reasonable range data %>% assert(within_bounds(ymd("2000-01-01"), today()), date) # Check that all values are within a reasonable range data %>% assert(within_bounds(0, 1000), value) # Check for duplicate rows data %>% assert(is_uniq(), date, value) } # Load and clean data data_cleaned <- load_and_clean_data("data.csv") # Perform QA steps perform_qa(data_cleaned) # Create plots for analysis data_cleaned %>% # Plot time series of values ggplot(aes(x = date, y = value)) + geom_line() + labs(title = "Time Series of Values", x = "Date", y = "Value") data_cleaned %>% # Plot distribution of values ggplot(aes(x = value)) + geom_histogram(bins = 30) + labs(title = "Distribution of Values", x = "Value", y = "Count") data_cleaned %>% # Plot values by month mutate(month = month(date, label = TRUE)) %>% ggplot(aes(x = month, y = value)) + geom_boxplot() + labs(title = "Values by Month", x = "Month", y = "Value") - User
What unit tests could I perform on this script to make sure it works as expected? Could you show me some examples?
- Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.