2016-06-09 40 views
1

我需要以某种方式转置我的数据。我会用一个例子来解释一下:使用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 

任何想法如何,我可以改进呢?

非常感谢

+0

尝试'库(dplyr)使用dcast;库(tidyr); data%>%group_by(Date)%>%unique()%>%spread(d,g)' – akrun

回答

3

我们可以在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 
相关问题