如何使用R重新排列表格

努杜

我需要使用下面的“表”获取以下“输出”。我下面的代码不起作用。感谢有人可以给我帮助。我尝试了这里提到的方法,但是没有运气获得我想要的输出。

谢谢。

table:
    
    GRID_CODE    sat_date       pol       my_id     acres
    0.1234       05_24_2019     12345     5         94.5
    0.5678       05_24_2019     12345     5         94.5
    0.1456       05_24_2019     12345     5         94.5
    0.5895       05_24_2019     12345     5         94.5
    1.2535       05_24_2019     12345     5         94.5
    0.4878       05_24_2019     12345     5         94.5
    0.2134       06_30_2019     12345     5         94.5
    0.6178       06_30_2019     12345     5         94.5
    0.1556       06_30_2019     12345     5         94.5
    0.5895       06_30_2019     12345     5         94.5
    0.2675       06_30_2019     12345     5         94.5
    0.7188       07_15_2019     12345     5         94.5
    0.8123       07_15_2019     12345     5         94.5
    0.1788       07_15_2019     12345     5         94.5
    0.9852       07_15_2019     12345     5         94.5
    0.4528       07_15_2019     12345     5         94.5
    0.7861       07_15_2019     12345     5         94.5
    0.0541       07_15_2019     12345     5         94.5
    

    

我下面的代码不起作用。感谢有人可以给我帮助。

    library(data.table)
    setDT(table)
    output1 <- dcast.data.table(table, pol + my_id + acres  ~ sat_date, fun.aggregate = identity, fill = NA_real_, value.var = "GRID_CODE")

pol     my_id   acres     05_24_2019     06_30_2019      07_15_2019 
12345   5       94.5      0.1234         0.2134          0.7188 
Aggregate function missing, defaulting to 'length'

我也试过了,但是没有产生想要的输出。

output2 <- reshape(out_p_sel, idvar = "pol", timevar = "sat_date", direction = "wide")


    output:
    
    pol       my_id    acres    05_24_2019       06_30_2019      07_15_2019
    12345     5        94.5     0.1234           0.2134          0.7188
    12345     5        94.5     0.5678           0.6178          0.8123
    12345     5        94.5     0.1456           0.1556          0.1788
    12345     5        94.5     0.5895           0.5895          0.9852
    12345     5        94.5     1.2535           0.2675          0.4528
    12345     5        94.5     0.4878           N/A             0.7861
    12345     5        94.5     N/A              N/A             0.0541
瓦西里

如果使用临时变量,则可以获得所需的输出:

table[, g1:=1:.N, by=sat_date]
output <- dcast(table, pol + my_id + acres + g1  ~ sat_date, fun.aggregate = identity, fill = NA_real_, value.var = "GRID_CODE")
output$g1 <- NULL

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章