r - 如何使用 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
解决方案
I think you need to specify the row number where each value goes. Try :
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
推荐阅读
- reactjs - 钩子是简化你的代码还是只是一个口味问题?
- python - 如何从 Watson Speech-to-Text 输出重建对话?
- bash - 如何递归更新文件名
- android - 如何将 gradle buildTypes 配置为仅在 jest 测试通过时运行?
- c - 加倍数字
- debugging - 是否有任何与 GDB 的“info proc mappings”等效的 LLDB 命令来显示进程的所有内存映射?
- celery - 如何在 celery beat 发布的 celery 任务中向消息或标头添加客户价值
- java - Javac JOptionPane 找不到符号:showMesssageDialog
- jsrender - JSViews 可观察数组在删除/添加和排序时不会刷新视图
- python - 数据框在读取 CSV 文件时导入列的第一个值作为列名