Consider 2 dataframes with identical column names and identical first column values.
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))
I want to insert rows 1-3 from 1st dataframe after row 3 in the other dataframe, rows 4-6 after row 6, rows 7-9 after row 9 and so on. rbind and bind_row functions do not seem to have any arguments that support such an operation.
Any help on how to do this is appreciated.
Edited to do every 3 rows.
I am not sure how general this is, but to avoid using a loop you can generate sequences with steps of two that repeat n times, and then merge and reorder the data. Probably not very elegant but it works with your data.
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),]
With the output:
> 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
Spliting the dataframes and recombinning them according to a sequence of 3s could accomplish your goals:
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
Another option is it to directly combine the two datasets and consequently reorganise the order of rows in the wished sequence as follows:
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)),]
This should yield the same outcome as r1
rownames(r1) <- 1:nrow(r1)
rownames(r2) <- 1:nrow(r2)
identical(r1, r2)
##[1] TRUE
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.