【问题标题】:Data Frame alternately row binding in RR中的数据框交替行绑定
【发布时间】:2018-12-14 14:59:49
【问题描述】:

我有两个数据框,它们具有完全相同的列和相同的行数。

我想创建一个新的数据框,它包含两个数据框但交替绑定的行。它必须从第一个数据框取一行,从第二个数据框取一行,直到构建完整的新数据框。

我尝试使用rbind() 没有运气。我需要一个不包括安装新 R 包的解决方案。

演示图片:

编辑:我的行数是动态的,可能非常大。此外,我需要一个不依赖列名的解决方案,因为结构也是动态的。我知道这两个数据框每次都具有相同的结构。

【问题讨论】:

  • 能否分享示例数据集? Bcoz 解决方案可能因数据集而异。简单的方法是为第一个df 添加奇数列,为第二个df 添加偶数列。 Merge这两个df并按数字排序。
  • @SaurabhChauhan 我的数据集不同,列名也在变化。但是每次我都有相同的结构两个数据集的列名和行大小。我需要一个通用的解决方案来组合相同的数据框,而不依赖于列名。
  • 我尝试使用 rbind() 没有运气。 ...为什么不呢?什么是不希望的结果或错误?
  • @Parfait 我想不正确的使用。应该像 Sotos 建议的那样使用 mapply。

标签: r rbind


【解决方案1】:

您可以将mapply 与 rbind 一起使用,即

d2 <- data.frame(a = c(4, 6, 8), b = c(letters[5:7]), stringsAsFactors = FALSE)
d1 <- data.frame(a = c(1, 2, 3), b = c(letters[1:3]), stringsAsFactors = FALSE)

mapply(rbind, d1, d2)
#      a   b  
#[1,] "1" "a"
#[2,] "4" "e"
#[3,] "2" "b"
#[4,] "6" "f"
#[5,] "3" "c"
#[6,] "8" "g"

【讨论】:

  • 我的行数是动态的,可以非常非常大。
  • 大小无所谓
  • 这可能会丢失原始数据帧的数据类型。
  • @nicola 确实,他们将不得不重组。
  • @nicola 谢谢你的警告,确实函数 as.matrix 被调用了,我丢失了类型。
【解决方案2】:

试试:

rbind(df1,df2)[rep(seq_len(nrow(df1)),each=2)+c(0,nrow(df1)),]

例子:

set.seed(1)
df1<-as.data.frame(matrix(runif(20),ncol=4))
#         V1         V2        V3        V4
#1 0.2655087 0.89838968 0.2059746 0.4976992
#2 0.3721239 0.94467527 0.1765568 0.7176185
#3 0.5728534 0.66079779 0.6870228 0.9919061
#4 0.9082078 0.62911404 0.3841037 0.3800352
#5 0.2016819 0.06178627 0.7698414 0.7774452
df2<-as.data.frame(matrix(runif(20),ncol=4))
#         V1         V2        V3        V4
#1 0.9347052 0.38611409 0.4820801 0.6684667
#2 0.2121425 0.01339033 0.5995658 0.7942399
#3 0.6516738 0.38238796 0.4935413 0.1079436
#4 0.1255551 0.86969085 0.1862176 0.7237109
#5 0.2672207 0.34034900 0.8273733 0.4112744
rbind(df1,df2)[rep(seq_len(nrow(df1)),each=2)+c(0,nrow(df1)),]
#          V1         V2        V3        V4
#1  0.2655087 0.89838968 0.2059746 0.4976992
#6  0.9347052 0.38611409 0.4820801 0.6684667
#2  0.3721239 0.94467527 0.1765568 0.7176185
#7  0.2121425 0.01339033 0.5995658 0.7942399
#3  0.5728534 0.66079779 0.6870228 0.9919061
#8  0.6516738 0.38238796 0.4935413 0.1079436
#4  0.9082078 0.62911404 0.3841037 0.3800352
#9  0.1255551 0.86969085 0.1862176 0.7237109
#5  0.2016819 0.06178627 0.7698414 0.7774452
#10 0.2672207 0.34034900 0.8273733 0.4112744

【讨论】:

    【解决方案3】:

    使用tidyverse 并使用@Sotos 的数据:

    d2 <- data.frame(a = c(4, 6, 8), b = c(letters[5:7]), stringsAsFactors = FALSE)
    d1 <- data.frame(a = c(1, 2, 3), b = c(letters[1:3]), stringsAsFactors = FALSE)
    
    library(tidyverse)
    lst(d1,d2) %>%
      map(rowid_to_column) %>% # add rowid to both tables
      bind_rows %>%            # bind
      arrange(rowid) %>%       # sort by id
      select(-rowid)           # clean up
    
    #   a b
    # 1 1 a
    # 2 4 e
    # 3 2 b
    # 4 6 f
    # 5 3 c
    # 6 8 g
    

    这是一个基本的替代方案

    do.call(rbind,
            Map(rbind,
                split(d1,seq(nrow(d1))),
                split(d2,seq(nrow(d2))))
            )
    #      a b
    # 1.1  1 a
    # 1.2  4 e
    # 2.2  2 b
    # 2.21 6 f
    # 3.3  3 c
    # 3.31 8 g
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-08
      • 1970-01-01
      • 2014-12-15
      相关资源
      最近更新 更多