I'm trying to take a column that has specific values for each type of element for each type of gridNumber
and dcast
it so that it creates 3 separate columns from the element column. I'm not sure exactly how to do this.
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 try:
newdat <- dcast(df, date ~ element)
Desired output columns:
date year month day gridNumber PPT TMAX TMIN value