簡體   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