d *总结了订单的重要性

时间:2012-01-26 09:04:11

标签: r plyr

以长格式考虑以下数据

library(plyr)
library(reshape2)

x <- seq(0,2*pi,length=20)
ll <- ll2 <- list(a = data.frame(x=x, y=sin(x)),
                  b = data.frame(x=x, y=cos(x)))

m <- melt(ll, id="x")
m2 <- m[sample(nrow(m)),]

head(m)
#         x variable     value L1
# 0.0000000        y 0.0000000  a
# 0.3306940        y 0.3246995  a
# 0.6613879        y 0.6142127  a
# 0.9920819        y 0.8371665  a
# 1.3227759        y 0.9694003  a
# 1.6534698        y 0.9965845  a

m$L1排序很好,但是m2$L1到处都是。现在,从这种数据开始,我想为value[L1 == "b"] - value[L1 == "a"]的每个值取差值x。以下几行说明了在diff未订购时使用m2$L1的问题:符号可能有误。有没有一个技巧可以用来实现最后两个ddply调用的结果,但更优雅?

res <- ddply(m, "x", summarize, difference = diff(value))
res <- ddply(m2, "x", summarize, difference = diff(value)) # fails, because L1 not ordered
res <- ddply(m2[order(m2$L1, m2$x), ], "x", summarize, difference = diff(value)) 
res <- ddply(m2, "x", function(d)
             data.frame(difference = d$value[d$L1 == "b"] - d$value[d$L1 == "a"]))

plot(res) # visual check of the result
lines(x, cos(x) - sin(x) , col="red")

1 个答案:

答案 0 :(得分:3)

dcast做你想做的事吗?

transform( 
  dcast( m2, x + variable ~ L1 ), 
  difference = b - a 
)