向另一个长度不等的数据框的每一行添加一个值

时间:2019-01-18 16:09:35

标签: r dataframe

我有以下数据集:

数据集1:

Class    Value
Yo       53
Save     13
Gold     72
Post     88

数据集2:

Class   Total_goals
Yo       9
Yo       9
Yo       9
Save     4
Save     4
Gold     7
Gold     7
Gold     7
Gold     7
Post     3
Post     3

我想要从第二个数据集中为Total_goals中的每个类添加dataset1

预期输出为:

Class    Value     Total_goals
Yo       53        9
Save     13        4
Gold     72        7
Post     88        3

我该怎么做?

1 个答案:

答案 0 :(得分:0)

使用 cbind ,无论您是否在数据集之间具有匹配变量,它都将起作用。这确实假设数据帧之间的观察级别相同(即顺序)。

创建数据框:

dataset1 = data.frame(c("yo","save","gold", "post"),c(53,13,72,88))
colnames(dataset1) = c("Class","Value")

dataset2 = data.frame(c("yo","save","gold", "post"),c(9,4,7,3))
colnames(dataset2) = c("Class","Total_goals")

答案:

dataset1 = cbind(dataset1, dataset2$Total_goals)
colnames(dataset1) = c("Class","Value","Total_goals")

*经过编辑以反映其他信息(即第二个数据框中的重复信息),需要匹配变量*

解决方案,如果数据帧的长度不相等,其中一个包含重复数据。

创建数据框:

dataset1= data.frame(c("yo","save","gold", "post"),c(53,13,72,88))
colnames(dataset1) = c("Class","Value")

dataset2 = data.frame(c("yo","save","gold", 
"post","post","gold"),c(9,4,7,3,3,7))
colnames(dataset2) = c("Class","Total_goals")

答案:

dataset1$Total_goal = dataset2[match(dataset1$Class, dataset2$Class),2]
colnames(dataset1) = c("Class","Value","Total_goals")