我需要以某种方式转置我的数据。我将用一个例子来解释它:
以下是数据:
data <- structure(list(Date = structure(c(1335724903, 1335724903, 1335724903, 1335724903),
class = c("POSIXct", "POSIXt"), tzone = ""),
a = c("UC 2", "UC 2", "UC 2", "UC 2"), b = c("50300", "50300", "50300", "50300"),
c = c("40", "40", "40", "40"), d = c("ISO_A","ISO_A", "ISO_B", "ISO_C"), e = c(2L, 2L, 2L, 2L),
f = c(45, 45, 45, 45), g = c(0.024, 0.024, 0.024, 0.024)),
.Names = c("Date", "a", "b", "c", "d", "e", "f", "g"), row.names = c(NA, 4L), class = "data.frame")
相同的数据,但只是更好的格式,所以我们可以通过“以某种方式转置”更好地了解我的意思:
Date a b c d e f g
1 2012-04-29 20:41:43 UC 2 50300 40 ISO_A 2 45 0.024
2 2012-04-29 20:41:43 UC 2 50300 40 ISO_A 2 45 0.024
3 2012-04-29 20:41:43 UC 2 50300 40 ISO_B 2 45 0.024
4 2012-04-29 20:41:43 UC 2 50300 40 ISO_C 2 45 0.024
所以从这张表中我想得到一张这样的表:
a b c d e f ISO_A ISO_B ISO_C
1 UC 2 50300 40 ISO_A 2 45 0.024 0.024 0.024
目前我对这段代码感到困惑:
data2 <- recast(data, a + b + c +d + e + f + variable ~ d, id.var = c("a","b","c","d","e","f"), fun.aggregate=mean)
导致我需要的表略有不同:
a b c d e f variable ISO_A ISO_B ISO_C
1 UC 2 50300 40 ISO_A 2 45 Date 1.335725e+09 NaN NaN
2 UC 2 50300 40 ISO_A 2 45 g 2.400000e-02 NaN NaN
3 UC 2 50300 40 ISO_B 2 45 Date NaN 1.335725e+09 NaN
4 UC 2 50300 40 ISO_B 2 45 g NaN 2.400000e-02 NaN
5 UC 2 50300 40 ISO_C 2 45 Date NaN NaN 1.335725e+09
6 UC 2 50300 40 ISO_C 2 45 g NaN NaN 2.400000e-02
我有什么想法可以改进它吗?
非常感谢
答案 0 :(得分:3)
我们可以在dcast
&#39;数据&#39;
unique
library(reshape2)
dcast(unique(data), ...~d, value.var="g", mean)
# Date a b c e f ISO_A ISO_B ISO_C
#1 2012-04-30 00:11:43 UC 2 50300 40 2 45 0.024 0.024 0.024