展开和弃置未正确对齐

时间:2016-03-02 03:29:27

标签: r reshape2 tidyr

我正在重新审视一些现在不起作用的代码,但我无法弄清楚为什么当我使用spreaddcast时,变量不会发生变化。与他们应该的日期保持一致。所有套餐都是最新的。

请注意,使用spread时,tmean位于自己的行中,而不是与1996-apr对齐。 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))

spreaddcast

> 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

1 个答案:

答案 0 :(得分:1)

您的问题与lat&amp; long

> unique(dat$long)
# [1] -73.45833 -73.45833
> unique(dat$lat)
# [1] 42.04167 42.04167

## Notice two 'unique' values that are 'printed' the same

如果您仔细查看structure数据,您会发现latlong

的值略有不同

例如,舍入lat / long值会消除精度&#39;错误&#39;

dat$lat  <- round(dat$lat, 4)
dat$long <- round(dat$long, 4)

spread(dat, element, value)

## now tmin is aligned 

    #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.4583 42.0417  1996   apr 6.642506 53.294 43.052 32.810
#2     266783  9005        0     CT Litchfield County -73.4583 42.0417  1996   aug 2.519680 76.550 66.551 56.552
#3     266783  9005        0     CT Litchfield County -73.4583 42.0417  1996   dec       NA     NA     NA 26.132