R 通过多列的值转换数据帧
我有一个需要基于多列中的值进行转换的数据帧。数据框如下所示:R 通过多列的值转换数据帧,r,dataframe,reshape,R,Dataframe,Reshape,我有一个需要基于多列中的值进行转换的数据帧。数据框如下所示: Cond Exp pathoffset ------------------------------- congruent standard 0.06819259 congruent standard 0.27370488 incongruent standard 0.34841871 incongruent standard 0.21540861 congruent forced 0.1948
Cond Exp pathoffset
-------------------------------
congruent standard 0.06819259
congruent standard 0.27370488
incongruent standard 0.34841871
incongruent standard 0.21540861
congruent forced 0.19483575
congruent forced 0.35533876
incongruent forced 0.19483575
incongruent forced 0.35533876
我一直试图获取的数据帧是
con_stand incon_stand con_for incon_for
----------------------------------------------
0.06819259 0.34841871 0.19483575 0.19483575
0.27370488 0.21540861 0.35533876 0.35533876
任何帮助都将不胜感激
谢谢添加“ID”变量后,您可以使用“重塑2”中的
dcast
mydf$id <- ave(mydf$Cond, mydf$Cond, mydf$Exp, FUN = seq_along)
library(reshape2)
dcast(mydf, id ~ Cond + Exp, value.var = "pathoffset")
# id congruent_forced congruent_standard incongruent_forced incongruent_standard
# 1 1 0.1948358 0.06819259 0.1948358 0.3484187
# 2 2 0.3553388 0.27370488 0.3553388 0.2154086
mydf$id使用第一列和第二列作为索引对第三列进行分组,然后使用sapply
或do。调用
展开列表
sapply(tapply(df[,3], paste(df[,1], df[,2], sep = "_"), c), "[")
或
do.call(cbind, tapply(df[,3], paste(df[,1], df[,2], sep = "_"), c))