合并不相等的数据帧?

时间:2019-04-06 01:20:08

标签: r

我正在尝试合并两个具有不同尺寸的数据框。问题总结如下,其中在一个表中有多个“香蕉”条目,而在另一个表中只有一个唯一条目。

我在下面的food_individual中有一个数据框,其中包含多个用于“香蕉”的条目:

food_individual = data.frame(name = c("Banana", "Banana", "Banana"), 
                             count = c(1, 3, 6), 
                             location = c("Germany", "Australia", "Tokyo"))

在下面的food_overall中,我只有一个唯一的“香蕉”条目:

food_overall = data.frame(name = "Banana", total_count = 10, colour = "Yellow")

这两个数据帧如下所示:

> food_individual
    name count  location
1 Banana     1   Germany
2 Banana     3 Australia
3 Banana     6     Tokyo

> food_overall
    name total_count colour
1 Banana          10 Yellow

我试图通过RStudio中的命令达到以下输出。我的预期结果应该像这样存储在combined_fruit中:

combined_fruit = data.frame(name = c("Banana", "Banana", "Banana"), 
                            count = c(1, 3, 6), 
                            location = c("Germany", "Australia", "Tokyo"), 
                            total_count = c(10, 10, 10), 
                            colour = c("Yellow", "Yellow", "Yellow"))
> combined_fruit
    name count  location total_count colour
1 Banana     1   Germany          10 Yellow
2 Banana     3 Australia          10 Yellow
3 Banana     6     Tokyo          10 Yellow

请帮助我,这让我发疯了!

0 个答案:

没有答案