我的数据集如下所示:
ID Meal Food Amount
1 Lunch Meat 50
1 Lunch Potato 10
1 Dinner Fish 105
1 Dinner Rice 100
1 Dinner Pulses 50
2 BF Cereal 100
2 BF Milk 200
2 Lunch Rice 200
2 Lunch Chicken 150
2 Lunch Veg 100
2 Dinner Pasta 200
2 Dinner Meat 200
2 Dinner Tomato 50
2 Dinner Cheese 10
我想使用变量Food,Meal和Amount中的信息来创建几个新变量,因此在一行中只有一个ID。 因此,我会有变量,如Cereal_BF,Cereal_Lunch,Cereal_Dinner,Meat_BF,Meat_Lunch,Meat_Dinner,Potato_BF,Potato_Lunch,Potato_Dinner等,它们显示来自变量Amount的相应信息。
感谢您的帮助!
帕特里夏
答案 0 :(得分:0)
您可以使用tidyverse
函数组合columuns并进行传播。
dd %>%
unite(MealFood, Meal, Food) %>%
spread(MealFood, Amount)
,其中
dd<-read.table(text="ID Meal Food Amount
1 Lunch Meat 50
1 Lunch Potato 10
1 Dinner Fish 105
1 Dinner Rice 100
1 Dinner Pulses 50
2 BF Cereal 100
2 BF Milk 200
2 Lunch Rice 200
2 Lunch Chicken 150
2 Lunch Veg 100
2 Dinner Pasta 200
2 Dinner Meat 200
2 Dinner Tomato 50
2 Dinner Cheese 10", header=T, stringsAsFactors=F)