在施法时保留变量

时间:2015-10-13 02:46:40

标签: r data.table

我试图将一些长数据转换为广泛数据,但无法弄清楚如何将某些变量附加到唯一ID。以下是我需要它做的事情,除了它删除附加到每个gridNumber的lat和long变量。我想在广泛的时候保留它们。

dput:

df <- structure(list(gridNumber = c("17578", "18982", "18983", "18984", 
"18985", "18986", "18987", "18988", "18989", "18990"), value = c(22.7000007629395, 
22.2900009155273, 22.25, 21.9799995422363, 21.1000003814697, 
20.7700004577637, 20.6200008392334, 20.5699996948242, 20.5699996948242, 
20.5799999237061), lat = c(-95.1249999994964, -95.1666666661633, 
-95.1249999994964, -95.0833333328295, -95.0416666661626, -94.9999999994957, 
-94.9583333328288, -94.9166666661619, -94.874999999495, -94.8333333328281
), long = c(49.4166666666667, 49.375, 49.375, 49.375, 49.375, 
49.375, 49.375, 49.375, 49.375, 49.375), ID = c("PRISM_ppt_stable_4kmM2_190001_bil", 
"PRISM_ppt_stable_4kmM2_190001_bil", "PRISM_ppt_stable_4kmM2_190001_bil", 
"PRISM_ppt_stable_4kmM2_190001_bil", "PRISM_ppt_stable_4kmM2_190001_bil", 
"PRISM_ppt_stable_4kmM2_190001_bil", "PRISM_ppt_stable_4kmM2_190001_bil", 
"PRISM_ppt_stable_4kmM2_190001_bil", "PRISM_ppt_stable_4kmM2_190001_bil", 
"PRISM_ppt_stable_4kmM2_190001_bil")), .Names = c("gridNumber", 
"value", "lat", "long", "ID"), class = c("data.table", "data.frame"
), row.names = c(NA, -10L))

代码:

library(data.table)
wide <- dcast.data.table(df, gridNumber~ID, value = 'value')

1 个答案:

答案 0 :(得分:7)

要解释@Frank评论(和正确答案),演员公式采用LHS ~ RHS形式。 LHS是您希望成为行键的列集,同样适用于RHS中的列。因此,如果您希望将gridNumber,lat和long作为每个行的唯一键,请将LHS设置为gridNumber + lat + long,如下所示:

wide <- dcast.data.table(df, gridNumber + lat + long ~ ID, value = 'value')
正如@Arun所指出的那样,

dcast可用于代替dcast.data.table(对于任何版本&gt; = 1.9.6,目前在CRAN上)。