微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何使用R

如何解决如何使用R

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

谢谢。

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

解决方法

我认为您需要指定每个值的行号。试试:

library(dplyr)

df %>%
  group_by(pol,my_id,sat_date) %>%
  mutate(row = row_number()) %>%
  tidyr::pivot_wider(names_from = sat_date,values_from = GRID_CODE) %>%
  select(-row)

#    pol my_id acres `05_24_2019` `06_30_2019` `07_15_2019`
#  <int> <int> <dbl>        <dbl>        <dbl>        <dbl>
#1 12345     5  94.5        0.123        0.213       0.719 
#2 12345     5  94.5        0.568        0.618       0.812 
#3 12345     5  94.5        0.146        0.156       0.179 
#4 12345     5  94.5        0.590        0.590       0.985 
#5 12345     5  94.5        1.25         0.268       0.453 
#6 12345     5  94.5        0.488       NA           0.786 
#7 12345     5  94.5       NA           NA           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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。