我正在尝试为每种类型的gridNumber
和dcast
选择一个具有每种元素的特定值的列,以便它从元素列中创建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
最佳答案
这可能不完全是您想要的,因为您有一个单独的值列。然后,您将PPT,TMAX和TMIN放在什么位置呢?
这是使用dplyr
和tidyr
将值放在适当列下的方法:
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)
关于r - dcast特定列并保留所有,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34234452/