dcast特定列并保留所有

时间:2015-12-11 23:49:49

标签: r reshape2

我尝试为每种类型的gridNumberdcast采用对每种元素类型都有特定值的列,以便它从元素列创建3个单独的列。我不确定该怎么做。

dput:

df <- structure(list(date = structure(c(-25584, -25584, -25584, -25583, 
-25583, -25583, -25582, -25582, -25582, -25581), class = "Date"), 
    year = c(1899, 1899, 1899, 1899, 1899, 1899, 1899, 1899, 
    1899, 1899), month = c(12, 12, 12, 12, 12, 12, 12, 12, 12, 
    12), day = c(15, 15, 15, 16, 16, 16, 17, 17, 17, 18), gridNumber = c(526228, 
    526228, 526228, 526228, 526228, 526228, 526229, 526229, 526229, 
    526229), element = c("PPT", "TMAX", "TMIN", "PPT", "TMAX", 
    "TMIN", "PPT", "TMAX", "TMIN", "PPT"), value = c(0, 43.4782, 
    21.7403, 0, 43.3297, 20.751, 0, 57.3625, 25.8157, 0.2105)), .Names = c("date", 
"year", "month", "day", "gridNumber", "element", "value"), row.names = c(NA, 
10L), class = "data.frame")

data.frame:

         date year month day gridNumber element   value
1  1899-12-15 1899    12  15     526228     PPT  0.0000
2  1899-12-15 1899    12  15     526228    TMAX 43.4782
3  1899-12-15 1899    12  15     526228    TMIN 21.7403
4  1899-12-16 1899    12  16     526228     PPT  0.0000
5  1899-12-16 1899    12  16     526228    TMAX 43.3297
6  1899-12-16 1899    12  16     526228    TMIN 20.7510
7  1899-12-17 1899    12  17     526229     PPT  0.0000
8  1899-12-17 1899    12  17     526229    TMAX 57.3625
9  1899-12-17 1899    12  17     526229    TMIN 25.8157
10 1899-12-18 1899    12  18     526229     PPT  0.2105
dcast尝试:

newdat <- dcast(df, date ~ element)

所需的输出列:

date year month day gridNumber PPT TMAX TMIN value

2 个答案:

答案 0 :(得分:10)

我们可以使用dcast...的lh上的~包括未在rhs和value.var中指定的所有变量。

library(reshape2)
dcast(df, ...~element, value.var='value')
#        date year month day gridNumber    PPT    TMAX    TMIN
#1 1899-12-15 1899    12  15     526228 0.0000 43.4782 21.7403
#2 1899-12-16 1899    12  16     526228 0.0000 43.3297 20.7510
#3 1899-12-17 1899    12  17     526229 0.0000 57.3625 25.8157
#4 1899-12-18 1899    12  18     526229 0.2105      NA      NA

答案 1 :(得分:1)

这可能不是您想要的,因为您有一个单独的值列。那么,你在PPT,TMAX和TMIN下放了什么?

以下是使用dplyrtidyr将值放在相应列下的方法:

library(dplyr)
library(tidyr)
df1 %>%
spread(element,value)
        date year month day gridNumber    PPT    TMAX    TMIN
1 1899-12-15 1899    12  15     526228 0.0000 43.4782 21.7403
2 1899-12-16 1899    12  16     526228 0.0000 43.3297 20.7510
3 1899-12-17 1899    12  17     526229 0.0000 57.3625 25.8157
4 1899-12-18 1899    12  18     526229 0.2105      NA      NA

只能使用tidyr一行编写:

spread(df1,element,value)