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

Transposing only few columns with multiple unique id.var using recast()

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

这是数据:

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")

相同的数据,但格式更好,因此我们可以更好地理解我所说的 "transposing in certain way":

               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

所以从这个table,我想得到一个像这样的table:

    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)

这导致我需要的 table 略有不同:

     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 'data'

上使用 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