在您的数据集中,还有一个名为“post”的变量,用于分组。
然而,我想我们不想包括:
main_df <- read.csv("https://raw.githubusercontent.com/rnorouzian/m2/main/g.csv")
index <- rev(expand.grid(outcome = unique(main_df$outcome), group =unique(main_df$group)))
df_fin <- function(index){
for (i in 1:nrow(index)) {
#creating a variable of each index row(group-outcome combination)
couple <- index[i,]
#subsetting one row of combination because they also are grouped by "post" variable
subsetted_df <- subset(main_df, outcome == couple[,2] & group == couple[,1])[1,]
name <- paste0("mpre", couple[,1],couple[,2])
assign(name, subsetted_df[,'mpre'])
name1 <- paste0("sdpre", couple[,1],couple[,2])
assign(name1, subsetted_df[,'sdpre'])
name2 <- paste0("n", couple[,1],couple[,2])
assign(name2, subsetted_df[,'n'])
}
final_df <- data.frame(
mpre1 = c(mpre11, mpre12),
sdpre1 = c(sdpre11, sdpre12),
n1 = c(n11, n12),
mpre2 = c(mpre21, mpre22),
sdpre2 = c(sdpre21, sdpre22),
n2 = c(n21, n22)
)
return(final_df)
}
desired_df <- df_fin(index)
desired_df
## mpre1 sdpre1 n1 mpre2 sdpre2 n2
## 1 0.21 0.28 21 0.12 0.25 16
## 2 1.61 1.50 21 1.13 0.92 16
这是你的意思吗?