【问题标题】:R-How to combine two dataframes with same column names but in different order [duplicate]R-如何组合具有相同列名但顺序不同的两个数据框[重复]
【发布时间】:2020-10-31 23:33:08
【问题描述】:

我想合并两个数据框,但顺序不同,RR 中缺少一些列。

这是一个例子:


df.1 <- data.frame(class = c(1,6,8,9,7,8,9,6,4), math = c(0.7, 0.4, 0.7), hist = c(0.6, 0.4, 0.3), geom = c(0.7, 0.4, 0.7), eng = c(0.7, 0.4, 0.7), draw = c(0.8, 0.6, 0.7))

df.2 <- data.frame(eng = c(0.7, 0.4, 0.7, class = c(2, 1, 2, 3, 1),draw = c(0.8, 0.6, 0.7),geom = c(0.7, 0.4, 0.7) )

谢谢

【问题讨论】:

  • 感谢您的回答。我不想 cbind 数据。我想合并它,但顺序不同,并且缺少一些列。对不起我的英语不好......
  • 如果R中的data.frames(垂直:添加更多行)的列数、名称和data.type不同,则无法合并。顺序无所谓。检查rbind()
  • 我使用了合并功能,但它不起作用
  • 您需要一些东西 - 例如,每个数据框中的一个额外列 - 来定义您想要连接两个数据框的顺序。然后使用某种形式的连接。没有额外的列,一个 id 列,R 没有它需要的信息来做你想做的事。根据您的样本数据了解您想要的结果将是有帮助的。
  • df.2 不是格式正确的数据框,您能否对其进行编辑使其成为有效对象?

标签: r dataframe sorting merge


【解决方案1】:

以下是我能得到的全部

class = c(1, 6, 8, 9, 7, 8, 9, 6, 4)
math = c(0.7, 0.4, 0.7)
hist = c(0.6, 0.4, 0.3)
geom = c(0.7, 0.4, 0.7)
eng = c(0.7, 0.4, 0.7)
draw = c(0.8, 0.6, 0.7)

dimension <-
  seq(max(
    length(class),
    length(math),
    length(hist),
    length(geom),
    length(eng),
    length(draw)
  ))

df.new <- data.frame(class[dimension], math[dimension], hist[dimension], 
                     geom[dimension], eng[dimension], draw[dimension])

names(df.new) <- c('class', 'math', 'hist', 'geom', 'eng', 'draw')

输出

df.new
  class math hist geom eng draw
1     1  0.7  0.6  0.7 0.7  0.8
2     6  0.4  0.4  0.4 0.4  0.6
3     8  0.7  0.3  0.7 0.7  0.7
4     9   NA   NA   NA  NA   NA
5     7   NA   NA   NA  NA   NA
6     8   NA   NA   NA  NA   NA
7     9   NA   NA   NA  NA   NA
8     6   NA   NA   NA  NA   NA
9     4   NA   NA   NA  NA   NA

【讨论】:

    【解决方案2】:

    这是你要找的吗?

    library(dplyr)
    
    df <- 
      full_join(df.1, df.2)
    #> Joining, by = c("class", "geom", "eng", "draw")
    
    df
    #>    class math hist geom eng draw
    #> 1      1  0.7  0.6  0.7 0.7  0.8
    #> 2      6  0.4  0.4  0.4 0.4  0.6
    #> 3      8  0.7  0.3  0.7 0.7  0.7
    #> 4      9  0.7  0.6  0.7 0.7  0.8
    #> 5      7  0.4  0.4  0.4 0.4  0.6
    #> 6      8  0.7  0.3  0.7 0.7  0.7
    #> 7      9  0.7  0.6  0.7 0.7  0.8
    #> 8      6  0.4  0.4  0.4 0.4  0.6
    #> 9      4  0.7  0.3  0.7 0.7  0.7
    #> 10     2   NA   NA  0.7 0.7  0.8
    #> 11     1   NA   NA  0.4 0.4  0.6
    #> 12     2   NA   NA  0.7 0.7  0.7
    #> 13     3   NA   NA  0.7 0.7  0.8
    #> 14     1   NA   NA  0.4 0.4  0.6
    #> 15     3   NA   NA  0.7 0.7  0.7
    

    reprex package (v0.3.0) 于 2020-07-11 创建

    数据

    df.1 <- data.frame(class = c(1,6,8,9,7,8,9,6,4), 
                       math = c(0.7, 0.4, 0.7), 
                       hist = c(0.6, 0.4, 0.3), 
                       geom = c(0.7, 0.4, 0.7), 
                       eng = c(0.7, 0.4, 0.7), 
                       draw = c(0.8, 0.6, 0.7))
    
    df.2 <- data.frame(eng = c(0.7, 0.4, 0.7), 
                       class = c(2, 1, 2, 3, 1, 3),
                       draw = c(0.8, 0.6, 0.7),
                       geom = c(0.7, 0.4, 0.7) )
    
    

    【讨论】:

      猜你喜欢
      • 2021-03-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-21
      • 2022-10-18
      • 2020-07-20
      • 2013-11-22
      • 1970-01-01
      相关资源
      最近更新 更多