在R数据框中生成按Subject分组的多变量组合均值数据框
Hey there! Let's work through this problem together—since you're new to R, I'll keep things clear and provide code you can test right away.
First, let's start with a simulated version of your data frame so you can follow along and verify the output:
# Simulate sample data (matches your structure) set.seed(123) # Makes results reproducible df <- data.frame( Subject = rep(1:6, each = 10), # 6 subjects, 10 rows each T = sample(0:1, 60, replace = TRUE), O = sample(0:1, 60, replace = TRUE), E = sample(0:1, 60, replace = TRUE), P = sample(0:1, 60, replace = TRUE), Score = rnorm(60, mean = 50, sd = 10) # Random numeric scores )
Approach 1: Using dplyr (Tidyverse, Recommended for Readability)
This method is intuitive and easy to modify if you need to add more combinations later. First, install and load the dplyr package if you haven't already:
install.packages("dplyr") library(dplyr)
Now, group the data by Subject and calculate the mean Score for each of your desired combinations:
result_df <- df %>% group_by(Subject) %>% summarise( # Single variables (mean Score where the variable equals 1) T = mean(Score[T == 1], na.rm = TRUE), O = mean(Score[O == 1], na.rm = TRUE), E = mean(Score[E == 1], na.rm = TRUE), P = mean(Score[P == 1], na.rm = TRUE), # Two-variable combinations (mean Score where both equal 1) TO = mean(Score[T == 1 & O == 1], na.rm = TRUE), TE = mean(Score[T == 1 & E == 1], na.rm = TRUE), TP = mean(Score[T == 1 & P == 1], na.rm = TRUE), OE = mean(Score[O == 1 & E == 1], na.rm = TRUE), OP = mean(Score[O == 1 & P == 1], na.rm = TRUE), PE = mean(Score[P == 1 & E == 1], na.rm = TRUE), # Three-variable combinations (mean Score where all equal 1) TOP = mean(Score[T == 1 & O == 1 & P == 1], na.rm = TRUE), TOE = mean(Score[T == 1 & O == 1 & E == 1], na.rm = TRUE), POE = mean(Score[P == 1 & O == 1 & E == 1], na.rm = TRUE), PET = mean(Score[P == 1 & E == 1 & T == 1], na.rm = TRUE) ) # View the result print(result_df)
The na.rm = TRUE argument ensures that if a subject has no rows matching a combination (e.g., no rows where T=1 and O=1), we get NA instead of an error—you can replace these NAs later with replace_na() from tidyr if needed.
Approach 2: Base R (No Extra Packages Needed)
If you prefer not to install new packages, you can use base R's by() function to process each subject separately, then combine the results:
# Process each subject and calculate means subject_results <- by(df, df$Subject, function(subject_data) { data.frame( Subject = unique(subject_data$Subject), T = mean(subject_data$Score[subject_data$T == 1], na.rm = TRUE), O = mean(subject_data$Score[subject_data$O == 1], na.rm = TRUE), E = mean(subject_data$Score[subject_data$E == 1], na.rm = TRUE), P = mean(subject_data$Score[subject_data$P == 1], na.rm = TRUE), TO = mean(subject_data$Score[subject_data$T == 1 & subject_data$O == 1], na.rm = TRUE), TE = mean(subject_data$Score[subject_data$T == 1 & subject_data$E == 1], na.rm = TRUE), TP = mean(subject_data$Score[subject_data$T == 1 & subject_data$P == 1], na.rm = TRUE), OE = mean(subject_data$Score[subject_data$O == 1 & subject_data$E == 1], na.rm = TRUE), OP = mean(subject_data$Score[subject_data$O == 1 & subject_data$P == 1], na.rm = TRUE), PE = mean(subject_data$Score[subject_data$P == 1 & subject_data$E == 1], na.rm = TRUE), TOP = mean(subject_data$Score[subject_data$T == 1 & subject_data$O == 1 & subject_data$P == 1], na.rm = TRUE), TOE = mean(subject_data$Score[subject_data$T == 1 & subject_data$O == 1 & subject_data$E == 1], na.rm = TRUE), POE = mean(subject_data$Score[subject_data$P == 1 & subject_data$O == 1 & subject_data$E == 1], na.rm = TRUE), PET = mean(subject_data$Score[subject_data$P == 1 & subject_data$E == 1 & subject_data$T == 1], na.rm = TRUE) ) }) # Combine all subject results into one data frame result_base <- do.call(rbind, subject_results) # View the result print(result_base)
Both methods will give you a 6-row data frame (one per subject) with all the mean values you need. The dplyr approach is cleaner for beginners, but the base R method works if you want to avoid package installs.
内容的提问来源于stack exchange,提问作者HelpASisterOut

