R rbind while preserving order or rows in each dat

2019-07-14 19:11发布

I need to merge 2 data frames while preserving their order of appearance in each data frame :

x = data.frame(a=1:3, b=2:4, c=3:5)
y = data.frame(a=c(10, 20, 30), b=c(20, 30, 40), c=c(30, 40, 50))

What I want is :

> z 

a   b   c
1   2   3
10  20  30
2   3   4
20  30  40
3   4   5
30  40  50

But what rbind does is it's adding the second dataframe under the first.

标签: r rbind
5条回答
小情绪 Triste *
2楼-- · 2019-07-14 19:35

Try this one-liner

do.call("rbind", Map("rbind", split(x, 1:nrow(x)), split(y, 1:nrow(y))))

which gives this data.frame if x and y are as in the question:

      a  b  c
1.1   1  2  3
1.2  10 20 30
2.2   2  3  4
2.21 20 30 40
3.3   3  4  5
3.31 30 40 50

It splits each data frame by row and then will rbind corresponding components of the splits. Then it rbinds all that. Note that this one-liner works even if the columns have different types. For example it will work even if:

x <- data.frame(a = letters[1:3], b = 1:3, c = c(TRUE, FALSE, TRUE))
y <- data.frame(a = LETTERS[1:3], b = 11:13, c = c(FALSE, TRUE, FALSE))
查看更多
太酷不给撩
3楼-- · 2019-07-14 19:36

As yet another base r version:

x = data.frame(a=1:3, b=2:4, c=3:5)
y = data.frame(a=c(10, 20, 30), b=c(20, 30, 40), c=c(30, 40, 50))

mapply(FUN = function(i,j){rbind(i,j)}, x, y)
#>       a  b  c
#> [1,]  1  2  3
#> [2,] 10 20 30
#> [3,]  2  3  4
#> [4,] 20 30 40
#> [5,]  3  4  5
#> [6,] 30 40 50

Created on 2019-02-14 by the reprex package (v0.2.1)

查看更多
叛逆
4楼-- · 2019-07-14 19:37

You could use the row number idea from Paweł in base R:

x$rowid <- seq(1, nrow(x)*2, by = 2) # or simply 1:nrow(x)
y$rowid <- seq(2, nrow(y)*2, by = 2)
z <- rbind(x, y)
z[order(z$rowid),]

   a  b  c rowid
1  1  2  3     1
4 10 20 30     2
2  2  3  4     3
5 20 30 40     4
3  3  4  5     5
6 30 40 50     6

edit can also use rownames generated by rbind():

z <- do.call(rbind, list(x = x, x = y))
z[order(rownames(z)), ]

      a  b  c
x.1   1  2  3
x.11 10 20 30
x.2   2  3  4
x.21 20 30 40
x.3   3  4  5
x.31 30 40 50
查看更多
狗以群分
5楼-- · 2019-07-14 19:42

You should add row number variable to each data frame and sort by this variable after binding. With dplyr you can do this as follow:

library(dplyr)

x %>%
  mutate(rn = row_number()) %>%
  bind_rows(
    y %>%
      mutate(rn = row_number())
  ) %>%
  arrange(rn)%>%
  select(-4)
查看更多
我欲成王,谁敢阻挡
6楼-- · 2019-07-14 19:44

Just for the sake of completeness, also a data.table solution.

library(data.table)
dt.x <- data.table(x)
dt.y <- data.table(y)
dt.x[,Row.Num :=seq(1:.N)]
dt.y[,Row.Num :=seq(1:.N)]
rbindlist(list(dt.x,dt.y), idcol = TRUE)[order(Row.Num),]
查看更多
登录 后发表回答