r - 在 r 中使用 cast、spread 或 melt 重塑数据表
问题描述
我有一个关于重塑表格的问题
这是我的表:
Dispatch_Table:
Name Year Month Day Hour Value
AA 2020 1 1 1 100
AA 2020 1 1 2 200
BB 2020 1 1 1 300
BB 2020 1 1 2 400
CC 2020 1 1 1 500
CC 2020 1 1 2 600
DD 2020 1 1 1 700
DD 2020 1 1 2 800
所需的表应该如下所示:
`Year Month Day Hour AA BB CC DD
2020 1 1 1 100 300 500 700
2020 1 1 2 200 400 600 800`
我尝试了cast
,mest
和spread
函数,但没有任何效果。每次都走错方向。我不明白如何在cast
和melt
函数中选择 ID 和变量。这些是我的尝试:
Reshaped_table<- reshape(Dispatch_Table, idvar = "Name", timevar c("Year","Month","Day","Hour"), direction = "wide")
reshaped<-melt(Dispatch_Table, id=c("Year"),direction = "wide")
reshaped<-dcast(Dispatch_Table, Name ~ ID, value.var ="value" )
reshaped<-spread(Dispatch_Table,Year,Month,Day,Hour)
我没有得到任何改进的解决方案。任何帮助将不胜感激。谢谢你。
解决方案
推荐阅读
- python - 为什么 emcee.EnsembleSampler.run_mcmc() 函数不会在我的任何步行者的连续步骤中改变我的 emcee.EnsembleSampler.chain 值?
- machine-learning - 概率编程与概率机器学习有什么区别?
- javascript - 在js中重新排序json
- c - 宏三元逗号表达式用作语句时会引起警告
- angular - 无法使用 FormArrayName 使用 FormBuilder 从输入中获取值
- mysql - 在 MySQL 中为 xml 路径使用函数的东西
- r - 用“for”循环连续保存一个变量
- c++ - 是否可以创建一个 C++ 程序,只链接到 libcstdc ++,而不链接到 libc?
- ios - AudioKit 无法录制麦克风
- c# - 当前上下文中不存在语法错误“助手”