可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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.
回答1:
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))
回答2:
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)
回答3:
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
回答4:
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)
回答5:
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),]