【问题标题】:How to create a new dataframe by merging columns with identical names from different dataframes in R?如何通过合并 R 中不同数据框中具有相同名称的列来创建新数据框?
【发布时间】:2020-10-26 12:39:44
【问题描述】:

在 R 中,我想从以下数据框中为每个列名创建一个新数据框:

agedf <- data.frame(A = c(12,14,16,18), B = c(13,15,17,19), C = c(11,13,15,17))
heightdf <- data.frame(A = c(110,120,130,140), B = c(120,130,140,150), C = c(115,125,135,145))
weightdf <- data.frame(A = c(80,90,100,110), B = c(90,100,110,120), C = c(85,95,105,115))

期望的结果是有一个公式为 A、B 和 C 中的每一个创建一个数据框,并分别使用它们的agedf、heightdf 和 weightdf 列。 IE。最终得到 3 个数据框,如这张 Excel 照片所示: Excel desired result

我怎样才能做到最好?

【问题讨论】:

    标签: r dataframe


    【解决方案1】:

    使用 for 循环(可能有像 tidyrdplyr 这样的封装替代品):

    newlist = list()
    names = colnames(agedf)
    for(i in names){
      index = which(colnames(agedf)==i)
      newlist[[i]] = cbind(agedf[,index], heightdf[,index], weightdf[,index])
      colnames(newlist[[i]]) = c("Age", "Height", "Weight")}
    

    输出:

    > newlist
    $A
         Age Height Weight
    [1,]  12    110     80
    [2,]  14    120     90
    [3,]  16    130    100
    [4,]  18    140    110
    
    $B
         Age Height Weight
    [1,]  13    120     90
    [2,]  15    130    100
    [3,]  17    140    110
    [4,]  19    150    120
    
    $C
         Age Height Weight
    [1,]  11    115     85
    [2,]  13    125     95
    [3,]  15    135    105
    [4,]  17    145    115
    

    不使用列表,并为每个 names 创建一个新的 df:

    names = colnames(agedf)
    for(i in names){
      index = which(colnames(agedf)==i)
      assign(i, cbind("Age"=agedf[,index], "Height"=heightdf[,index], "Weight"=weightdf[,index]))}
    

    这给出了与之前相同的输出,只是不在列表中。

    最后,如果要将它们全部添加到单个数据框中,并指定每个观察值的来源:

    df = numeric()
    names = colnames(agedf)
    for(i in names){
      index = which(colnames(agedf)==i)
      df = rbind(df, cbind(i, agedf[,index], heightdf[,index], weightdf[,index]))}
    colnames(df) = c("Code", "Age", "Height", "Weight")
    df = as.data.frame(df)
    

    输出:

    > df
       Code Age Height Weight
    1     A  12    110     80
    2     A  14    120     90
    3     A  16    130    100
    4     A  18    140    110
    5     B  13    120     90
    6     B  15    130    100
    7     B  17    140    110
    8     B  19    150    120
    9     C  11    115     85
    10    C  13    125     95
    11    C  15    135    105
    12    C  17    145    115
    

    Obs:您也可以将 colnames c("Age", "Height", "Weight") 直接传递到 cbind 中。

    【讨论】:

      【解决方案2】:

      这是一种使用tidyverse的方法。

      library(dplyr)
      library(purrr)
      
      df_list <- list(Age = agedf,
                      Height = heightdf,
                      Weight = weightdf)
      
      map(transpose(df_list), bind_cols)
      
      # $A
      # # A tibble: 4 x 3
      # Age Height Weight
      # <dbl>  <dbl>  <dbl>
      #   1    12    110     80
      #   2    14    120     90
      #   3    16    130    100
      #   4    18    140    110
      # 
      # $B
      # # A tibble: 4 x 3
      # Age Height Weight
      # <dbl>  <dbl>  <dbl>
      #   1    13    120     90
      #   2    15    130    100
      #   3    17    140    110
      #   4    19    150    120
      # 
      # $C
      # # A tibble: 4 x 3
      # Age Height Weight
      # <dbl>  <dbl>  <dbl>
      #   1    11    115     85
      #   2    13    125     95
      #   3    15    135    105
      #   4    17    145    115
      

      【讨论】:

        猜你喜欢
        • 2023-01-22
        • 2021-07-13
        • 1970-01-01
        • 1970-01-01
        • 2019-01-31
        • 1970-01-01
        • 2021-03-30
        • 1970-01-01
        • 2021-03-11
        相关资源
        最近更新 更多