Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/75.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 通过多列的值转换数据帧_R_Dataframe_Reshape - Fatal编程技术网

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))