如何添加为现有行添加新行的列

时间:2019-03-28 14:59:35

标签: r

我有两个数据帧df1和df2,并希望将它们合并以形成df3,如下所示。我敢肯定有一个简单的方法可以做到这一点,但是我一直无法找到直接的解决方案。

df1 = data.frame(id = c(1,2), Name = c('Bob', 'Sue'), stringsAsFactors = F)
id | Name 
==========
1 |   Bob 
2 |   Sue 

df2 = data.frame(id = c(1,2,3,4), year = c(2001, 2002, 2003, 2004))
id | year
==========
1 |   2001 
2 |   2002 
3 |   2003 
4 |   2004

df3 =
id | Name | year
=================
1 |   Bob | 2001
2 |   Bob | 2002
3 |   Bob | 2003
4 |   Bob | 2004
5 |   Sue | 2001
6 |   Sue | 2002
7 |   Sue | 2003
8 |   Sue | 2004

3 个答案:

答案 0 :(得分:6)

merge(df1, df2, by=NULL)用于笛卡尔积 看到这里:https://www.rdocumentation.org/packages/base/versions/3.5.3/topics/merge

答案 1 :(得分:4)

我们可以使用crossing

library(dplyr)
library(tidyr)
crossing(df1, df2) %>%
   transmute(id = row_number(), Name, year)
#  id Name year
#1  1  Bob 2001
#2  2  Bob 2002
#3  3  Bob 2003
#4  4  Bob 2004
#5  5  Sue 2001
#6  6  Sue 2002
#7  7  Sue 2003
#8  8  Sue 2004

似乎输出中的“ id”列与数据集中的初始“ id”列无关。在这种情况下,请在不添加“ id”列的情况下执行crossing,然后将“ id”创建为row_number()

crossing(df1[-1], df2[-1]) %>% 
        mutate(id = row_number())

数据

df1 <- structure(list(id = 1:2, Name = c("Bob", "Sue")), 
  class = "data.frame", row.names = c(NA, -2L))

df2 <- structure(list(id = 1:4, year = 2001:2004), class = "data.frame",
 row.names = c(NA, -4L))

答案 2 :(得分:2)

也许您可以使用:expand.grid(Name = df1$Name, year = df2$year)

给出:

  Name year
1  Bob 2001
2  Sue 2001
3  Bob 2002
4  Sue 2002
5  Bob 2003
6  Sue 2003
7  Bob 2004
8  Sue 2004