R - 在每个“n”行之后将行从一个数据帧合并到另一个数据帧

问题描述 投票:2回答:2

考虑具有相同列名和相同第一列值的2个数据帧。

df1 <- data.frame(col1 = rep(c("x", "y", "z"),4),
                col2 = as.factor(sample(12)),
                col3 = sample(c(TRUE, FALSE), 12, replace = TRUE))
df2 <- data.frame(col1 = rep(c("x", "y", "z"),4),
                col2 = as.factor(sample(12)),
                col3 = sample(c(TRUE, FALSE), 12, replace = TRUE))

我想在第3行之后的第1个数据帧中插入1-3行,在第6行之后插入行4-6,在第9行之后插入第7-9行,依此类推。 rbind和bind_row函数似乎没有任何支持此类操作的参数。

任何有关如何做到这一点的帮助表示赞赏。

r dataframe rows
2个回答
3
投票

编辑每3行做一次。

我不确定这是多么普遍,但为了避免使用循环,您可以生成重复n次的步骤为2的序列,然后合并并重新排序数据。可能不是很优雅,但它适用于您的数据。

step=3
df1$col4<-rep(seq(from=1, to=dim(df1)[1]/step*2,by=2), each=step)
df2$col4<-rep(seq(from=2, to=dim(df2)[1]/step*2,by=2), each=step)
df<-rbind(df1,df2)
df<-df[order(df$col4),]

随着输出:

> step=3
> df1$col4<-rep(seq(from=1, to=dim(df1)[1]/step*2,by=2), each=step)
> df1$col4
 [1] 1 1 1 3 3 3 5 5 5 7 7 7
> df2$col4<-rep(seq(from=2, to=(dim(df2)[1]/step)*2,by=2), each=step)
> df2$col4
 [1] 2 2 2 4 4 4 6 6 6 8 8 8
> df<-rbind(df1,df2)
> df
   col1 col2  col3 col4
1     x    7  TRUE    1
2     y    8 FALSE    1
3     z    3 FALSE    1
4     x    5 FALSE    3
5     y    9 FALSE    3
6     z    6  TRUE    3
7     x    4  TRUE    5
8     y   11  TRUE    5
9     z   12  TRUE    5
10    x    2  TRUE    7
11    y    1  TRUE    7
12    z   10  TRUE    7
13    x    1 FALSE    2
14    y    5 FALSE    2
15    z   10  TRUE    2
16    x    7  TRUE    4
17    y   11  TRUE    4
18    z    8  TRUE    4
19    x    2 FALSE    6
20    y   12  TRUE    6
21    z    9 FALSE    6
22    x    4 FALSE    8
23    y    6 FALSE    8
24    z    3  TRUE    8
> df<-df[order(df$col4),]
> df
   col1 col2  col3 col4
1     x    7  TRUE    1
2     y    8 FALSE    1
3     z    3 FALSE    1
13    x    1 FALSE    2
14    y    5 FALSE    2
15    z   10  TRUE    2
4     x    5 FALSE    3
5     y    9 FALSE    3
6     z    6  TRUE    3
16    x    7  TRUE    4
17    y   11  TRUE    4
18    z    8  TRUE    4
7     x    4  TRUE    5
8     y   11  TRUE    5
9     z   12  TRUE    5
19    x    2 FALSE    6
20    y   12  TRUE    6
21    z    9 FALSE    6
10    x    2  TRUE    7
11    y    1  TRUE    7
12    z   10  TRUE    7
22    x    4 FALSE    8
23    y    6 FALSE    8
24    z    3  TRUE    8

1
投票

拆分数据框并按照3的顺序组合它们可以实现您的目标:

df1_split <- split(df1, rep(1:(nrow(df1)/3), each = 3))
df2_split <- split(df2, rep(1:(nrow(df1)/3), each = 3))
r1 <- do.call(rbind, lapply(seq_along(df1_split), function(i) rbind(df2_split[[i]], df1_split[[i]])))

#    col1 col2  col3
#1      x    9  TRUE
#2      y   10 FALSE
#3      z    4  TRUE
#4      x   12  TRUE
#5      y    9 FALSE
#6      z    8 FALSE
#42     x   12 FALSE
#52     y    1 FALSE
#62     z    2  TRUE
#41     x    1 FALSE
#51     y    2  TRUE
#61     z   10 FALSE
#7      x    8  TRUE
#8      y    3  TRUE
#9      z    7  TRUE
#71     x    5  TRUE
#81     y    7 FALSE
#91     z   11 FALSE
#10     x    5 FALSE
#11     y   11 FALSE
#12     z    6  TRUE
#101    x    3 FALSE
#111    y    6 FALSE
#121    z    4 FALSE

另一种选择是直接组合两个数据集,然后重新组织所需序列中的行顺序,如下所示:

S <- seq(3, nrow(df2)+nrow(df1), by = 6)
seqDF2 <- unlist(Map(seq, S-2, S))
seqDF1 <- setdiff(1:(nrow(df2)+nrow(df1)), seqDF2)
r2 <- rbind(df2, df1)[match(1:(nrow(df2)+nrow(df1)), c(seqDF2, seqDF1)),]

这应该产生与r1相同的结果

rownames(r1) <- 1:nrow(r1)
rownames(r2) <- 1:nrow(r2)
identical(r1, r2)
##[1] TRUE
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.