如何通过合并r中的两个数据帧以堆叠格式重塑数据?
R中是否有任何函数可以通过合并两个数据帧以堆叠格式重塑数据 表1如何通过合并r中的两个数据帧以堆叠格式重塑数据?,r,R,R中是否有任何函数可以通过合并两个数据帧以堆叠格式重塑数据 表1 Vegetables|Onion|Potato|Tomato| Cheap | 20 | 30 | 40 | 表2 Vegetables|Cabbage|Carrot|Cauliflower|Eggplant Mid | 20 | 30 | 40 | 30 所需输出 Vegetables | Type |Quantity| Cheap |
Vegetables|Onion|Potato|Tomato|
Cheap | 20 | 30 | 40 |
表2
Vegetables|Cabbage|Carrot|Cauliflower|Eggplant
Mid | 20 | 30 | 40 | 30
所需输出
Vegetables | Type |Quantity|
Cheap | Onion | 20 |
Cheap | Potato | 30 |
Cheap | Tomato | 40 |
Mid | Cabbage | 20 |
Mid | Carrot | 30 |
Mid |Cauliflower| 40 |
Mid | Eggplant | 30 |
提前感谢您的帮助!您太棒了。您尝试使用的参数名称是
id.vars
,而不是id
。尝试melt(表1,id.vars=“蔬菜”)
library(重塑2)
df1您并不是真的在这里合并。您实际上只是将它们组合或连接起来。查看“熔化”——从中提取数据以将列转换为行。是的,我做了一些更改。我也试过用melt。你能给我举个例子,比如如何实现它。如果你能展示你的尝试并描述它是如何失败的,那就更好了。如果您在a中提供数据以使其更易于复制/粘贴到R中,则更容易提供帮助。感谢您的建议,我尝试了melt(Table1,id=“Vescelles”)来实现这一目的,但在我的R环境ls()中,我已经找到了100个这样的数据框。您能告诉我如何将相同的数据帧应用于所有数据帧,并将它们合并到一个表中吗!
library(reshape2)
df1 <- data.frame(Vegetables="Cheap", Onion=20,Potato=30,Tomato=40)
df2 <- data.frame(Vegetables="Mid", Cabbage=20, Carrot=30,Cauliflower=40, Eggplant=30)
do.call("rbind", lapply(list(df1,df2),function(d){melt(d,id.vars="Vegetables", variable.name="Type", value.name="Quantity")}))