繁体   English   中英

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

[英]R - Merge rows from one dataframe to another dataframe after every “n”th row

考虑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-3行,在第6行之后插入第4-6行,在第9行之后插入第7-9行,依此类推。 rbind和bind_row函数似乎没有支持这种操作的任何参数。

感谢您提供有关如何执行此操作的帮助。

编辑为每3行做一次。

我不确定这有多通用,但是要避免使用循环,您可以生成两个步长重复n次的序列,然后对数据进行合并和重新排序。 可能不是很优雅,但是可以处理您的数据。

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

拆分数据帧并按照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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM