R减法表示基于另一个变量/列的一个变量/列

时间:2016-03-21 22:48:05

标签: r

我有一个包含反应时间值的数据框df,我想根据Congruency"congruent" and "incongruent"中的两个值减去平均反应时间。 然而,我还想基于其他变量的其他变量,特别是一次几个变量来做这种减法。

MWE:

   df <- data.frame(rt = c(764.2, 629.7, 629.7, 412.1, 538.1, 606.2, 348.7, 532.5, 436.7,490.2),
                          Congruency = c("incongruent", "congruent","congruent", "incongruent", "incongruent", "incongruent", "congruent","congruent", "incongruent","congruent"),
                          Position = c("left", "left","right", "right", "right", "left", "left", "left", "right", "right"),
                          Variable = c(1, 1, 2, 3, 3, 2, 3, 1, 2, 1))

我试图使用SummarySE和子集函数,但计算很冗长,我确信有更好的解决方案。

library(Rmisc)
newdf<-data.frame(interference=c(0,0),Position =c("left","right"))
dfc <-summarySE(df, measurevar="rt",groupvars=c("Congruency","Position"))

left<-mean(subset(dfc$rt,dfc$Congruency == "congruent" & dfc$Position == "left")) - mean(subset(dfc$rt,dfc$Congruency == "incongruent"&  dfc$Position == "left"))
right<-mean(subset(dfc$rt,dfc$Congruency == "congruent"&  dfc$Position == "right")) - mean(subset(dfc$rt,dfc$Congruency == "incongruent"& dfc$Position == "right"))

newdf$interference <- c(left,right)

预期产出:

> newdf
  interference Position
1    -186.5667     left
2      97.6500    right

感谢阅读

0 个答案:

没有答案