claude-sonnet-4-20250514-thinking-32k· Anthropic LMArena Human Preference 140K
write a function in r to analyze a data set with 5 columns, caseid, 3 controls (control…
- User
write a function in r to analyze a data set with 5 columns, caseid, 3 controls (control 1,2,3) and value
- Assistant
Here's an R function to analyze a dataset with the structure you described:
analyze_dataset <- function(data, plot = TRUE) { # Load required libraries if (!require(dplyr)) install.packages("dplyr") if (!require(ggplot2)) install.packages("ggplot2") library(dplyr) library(ggplot2) # Validate column names expected_cols <- c("caseid", "control1", "control2", "control3", "value") if (!all(expected_cols %in% names(data))) { stop("Dataset must contain columns: caseid, control1, control2, control3, value") } cat("=== DATASET ANALYSIS ===\n\n") # 1. Basic dataset info cat("Dataset Overview:\n") cat("- Number of cases:", nrow(data), "\n") cat("- Number of columns:", ncol(data), "\n") cat("- Missing values by column:\n") print(sapply(data, function(x) sum(is.na(x)))) cat("\n") # 2. Summary statistics for value variable cat("Value Variable Summary:\n") print(summary(data$value)) cat("Standard Deviation:", sd(data$value, na.rm = TRUE), "\n\n") # 3. Control variables summary cat("Control Variables Summary:\n") for (i in 1:3) { col_name <- paste0("control", i) cat(paste("Control", i, ":\n")) if (is.numeric(data[[col_name]])) { print(summary(data[[col_name]])) } else { print(table(data[[col_name]], useNA = "ifany")) } cat("\n") } # 4. Value statistics by each control variable cat("Value by Control Variables:\n") for (i in 1:3) { col_name <- paste0("control", i) cat(paste("Mean Value by Control", i, ":\n")) result <- data %>% group_by(!!sym(col_name)) %>% summarise( mean_value = mean(value, na.rm = TRUE), median_value = median(value, na.rm = TRUE), sd_value = sd(value, na.rm = TRUE), count = n(), .groups = 'drop' ) print(result) cat("\n") } # 5. Correlation analysis (for numeric controls) numeric_cols <- sapply(data[c("control1", "control2", "control3", "value")], is.numeric) if (sum(numeric_cols) > 1) { cat("Correlation Matrix (numeric variables only):\n") cor_matrix <- cor(data[c("control1", "control2", "control3", "value")][numeric_cols], use = "complete.obs") print(round(cor_matrix, 3)) cat("\n") } # 6. Generate plots if requested if (plot) { # Distribution of value variable p1 <- ggplot(data, aes(x = value)) + geom_histogram(bins = 30, fill = "skyblue", alpha = 0.7) + labs(title = "Distribution of Value Variable", x = "Value", y = "Frequency") + theme_minimal() # Boxplots for value by each control plot_list <- list() for (i in 1:3) { col_name <- paste0("control", i) p <- ggplot(data, aes_string(x = col_name, y = "value")) + geom_boxplot(fill = "lightcoral", alpha = 0.7) + labs(title = paste("Value by Control", i), x = paste("Control", i), y = "Value") + theme_minimal() + theme(axis.text.x = element_text(angle = 45, hjust = 1)) plot_list[[i]] <- p } # Display plots print(p1) for (p in plot_list) { print(p) } } # 7. Return summary object invisible(list( dataset_info = list( n_cases = nrow(data), missing_values = sapply(data, function(x) sum(is.na(x))) ), value_summary = summary(data$value), correlations = if(sum(numeric_cols) > 1) cor_matrix else NULL )) } # Example usage: # result <- analyze_dataset(your_data) # result <- analyze_dataset(your_data, plot = FALSE) # without plotsThis function provides:
Basic Analysis:
- Dataset overview (dimensions, missing values)
- Descriptive statistics for the value variable
- Summary of each control variable
Advanced Analysis:
- Mean/median value grouped by each control variable
- Correlation matrix for numeric variables
- Visual plots (histogram of values, boxplots by controls)
Usage Examples:
# With plots (default) results <- analyze_dataset(my_data) # Without plots results <- analyze_dataset(my_data, plot = FALSE) # The function returns a summary object you can access: results$value_summary results$correlationsThe function automatically handles different data types in the control variables and provides appropriate summaries for both categorical and numeric controls.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.