我正在重新訪問一些現在不起作用的代碼,我無法弄清楚爲什麼當我使用spread
或dcast
時,變量與日期不一致。所有軟件包都是最新的。傳播和dcast未正確對齊
請注意,當使用spread
時,tmean
在其自己的行中,而不是與1996年4月對齊。 dcast
做同樣的事情。
下面是一個例子:
dput:
library(reshape2)
library(tidyr)
dat <- structure(list(gridNumber = c(266783L, 266783L, 266783L, 266783L,
266783L, 266783L, 266783L, 266783L, 266783L), fips = c(9005L,
9005L, 9005L, 9005L, 9005L, 9005L, 9005L, 9005L, 9005L), cropArea = c(0,
0, 0, 0, 0, 0, 0, 0, 0), state = structure(c(8L, 8L, 8L, 8L,
8L, 8L, 8L, 8L, 8L), .Label = c("AK", "AL", "AR", "AS", "AZ",
"CA", "CO", "CT", "DC", "DE", "FL", "GA", "GU", "HI", "IA", "ID",
"IL", "IN", "KS", "KY", "LA", "MA", "MD", "ME", "MI", "MN", "MO",
"MP", "MS", "MT", "NC", "ND", "NE", "NH", "NJ", "NM", "NV", "NY",
"OH", "OK", "OR", "PA", "PR", "RI", "SC", "SD", "TN", "TX", "UM",
"UT", "VA", "VI", "VT", "WA", "WI", "WV", "WY"), class = "factor"),
county_name = c("Litchfield County", "Litchfield County",
"Litchfield County", "Litchfield County", "Litchfield County",
"Litchfield County", "Litchfield County", "Litchfield County",
"Litchfield County"), long = c(-73.4583333333292, -73.4583333333292,
-73.4583333333292, -73.458333332921, -73.4583333333292, -73.4583333333292,
-73.4583333333292, -73.458333332921, -73.4583333333292),
lat = c(42.0416666666681, 42.0416666666681, 42.0416666666681,
42.041666666606, 42.0416666666681, 42.0416666666681, 42.0416666666681,
42.041666666606, 42.0416666666681), year = c(1996L, 1996L,
1996L, 1996L, 1996L, 1996L, 1996L, 1996L, 1996L), element = c("tmin",
"tmax", "ppt", "tmean", "tmin", "tmax", "ppt", "tmean", "tmin"
), month = c("apr", "apr", "apr", "apr", "aug", "aug", "aug",
"aug", "dec"), value = c(32.8099999785423, 53.2939998626709,
6.64250644805908, 43.0519997596741, 56.552000617981, 76.55,
2.51968, 66.5509994506836, 26.1320000171661)), .Names = c("gridNumber",
"fips", "cropArea", "state", "county_name", "long", "lat", "year",
"element", "month", "value"), class = c("tbl_df", "data.frame"
), row.names = c(NA, -9L))
spread
和dcast
:
> spread(dat, element, value)
Source: local data frame [5 x 13]
gridNumber fips cropArea state county_name long lat year month ppt tmax tmean tmin
(int) (int) (dbl) (fctr) (chr) (dbl) (dbl) (int) (chr) (dbl) (dbl) (dbl) (dbl)
1 266783 9005 0 CT Litchfield County -73.45833 42.04167 1996 apr 6.642506 53.294 NA 32.810
2 266783 9005 0 CT Litchfield County -73.45833 42.04167 1996 aug 2.519680 76.550 NA 56.552
3 266783 9005 0 CT Litchfield County -73.45833 42.04167 1996 dec NA NA NA 26.132
4 266783 9005 0 CT Litchfield County -73.45833 42.04167 1996 apr NA NA 43.052 NA
5 266783 9005 0 CT Litchfield County -73.45833 42.04167 1996 aug NA NA 66.551 NA
> dcast(dat, gridNumber + fips + cropArea + state + county_name + long + lat + year + month ~ element, value.var = "value")
gridNumber fips cropArea state county_name long lat year month ppt tmax tmean tmin
1 266783 9005 0 CT Litchfield County -73.45833 42.04167 1996 apr 6.642506 53.294 NA 32.810
2 266783 9005 0 CT Litchfield County -73.45833 42.04167 1996 aug 2.519680 76.550 NA 56.552
3 266783 9005 0 CT Litchfield County -73.45833 42.04167 1996 dec NA NA NA 26.132
4 266783 9005 0 CT Litchfield County -73.45833 42.04167 1996 apr NA NA 43.052 NA
5 266783 9005 0 CT Litchfield County -73.45833 42.04167 1996 aug NA NA 66.551 NA
我認爲問題在於您使用數字列作爲鍵。試試'sprintf(「%。40f」,dat $ long)'來看看我的意思。 Yup; – bdemarest