【发布时间】:2015-02-09 16:54:55
【问题描述】:
下面的问题可以看成是“两列reshape to wide”,有几种方法可以用经典的方式解决,从base::reshape(恐怖)到reshape2。对于两组情况,简单的子组连接效果最好。
我可以在dplyr 的管道框架内重新定义连接吗?下面的例子有点傻,但我需要加入更长的管道链,我不想破坏它。
library(dplyr)
d = data.frame(subject= rep(1:5,each=2),treatment=letters[1:2],bp = rnorm(10))
d %>%
# Assume piped manipulations here
# Make wide
# Assume additional piped manipulations here
# Make wide (old style)
with(d,left_join(d[treatment=="a",],
d[treatment=="b",],by="subject" ))
【问题讨论】:
-
也许您可以详细说明“简单的子组加入效果最好”部分?是什么阻止你把例如
d %>% dcast(subject ~ treatment, value.var = "bp")在你的管道里?似乎left_join需要更多的硬编码?但我可能误解了你的目标。 -
你理解得很好,我可能是因为过去太多的重塑而大脑受损。有效(就像@beginneR 的传播一样)。