r语言 - 使用 recast() 仅转置具有多个唯一 id.var 的少数列



我需要以某种方式转置我的数据。我将用一个例子来解释它:

以下是数据:

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

任何想法如何改进它?

非常感谢

我们可以在

unique"数据"上使用dcast

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

最新更新