【问题标题】:Join data.frames by id where the id is the name of the data frame, and keep those ids in the rows通过 id 连接 data.frames,其中 id 是数据框的名称,并将这些 id 保留在行中
【发布时间】:2015-09-08 12:45:37
【问题描述】:

我有一个 data.frames 列表,其中 data.frames 的名称包含 ID 以及“分组”特征。我想加入共享名称的“特征”部分的 data.frames,并将 ids 保留为这个新 data.frame 上的行 ids。很难用语言表达,这里有一个 MRE:

trying <- list(" 8 type1"= data.frame(values = c(1:3), date =c("2015-08-08","2015-08-07","2015-08-06")),
           " 8 type2"= data.frame(values = c(4:6), date =c("2015-03-04","2015-03-03","2015-03-02")),
           " 9 type1"= data.frame(values = c(3:5), date =c("2015-05-03","2015-05-02","2015-05-01")),
           " 9 type2"= data.frame(values = c(2:4), date =c("2015-02-01","2015-01-31","2015-01-30")))

我的预期输出是两个按类型连接的 data.frame 的列表,其中 id 保存为行:

tryingmodified <- list("type1" = data.frame(id = c(8,8,8,9,9,9), values = c(1:3,3:5), date = c("2015-08-08","2015-08-07","2015-08-06", "2015-05-03","2015-05-02","2015-05-01")),
                   "type2" = data.frame(id = c(8,8,8,9,9,9), values = c(4:6,2:4), date = c("2015-03-04","2015-03-03","2015-03-02", "2015-02-01","2015-01-31","2015-01-30")))

请注意,我的实际数据比这大得多(更多行、更多 data.frames 和更多列),实际名称不是 'typeX' 但一般结构占优势'space INT space TEXT': ("8 type1 ”)。因此,任何易于扩展的解决方案都是可取的。

【问题讨论】:

    标签: r dataframe


    【解决方案1】:

    使用 dplyr,您可以将结果作为数据框获得,而不是作为列表,就像在 trymodified 中一样。我不确定这是否是个问题。

    trying <- list(" 8 type1"= data.frame(values = c(1:3), date =c("2015-08-08","2015-08-07","2015-08-06")),
         " 8 type2"= data.frame(values = c(4:6), date =c("2015-03-04","2015-03-03","2015-03-02")),
         " 9 type1"= data.frame(values = c(3:5), date =c("2015-05-03","2015-05-02","2015-05-01")),
         " 9 type2"= data.frame(values = c(2:4), date =c("2015-02-01","2015-01-31","2015-01-30")))
    
    
    library(dplyr) # dplyr version 0.43
    df <- bind_rows(trying, .id = "ids") %>% 
          mutate(id = gsub(" type.", "", ids), type = gsub("^ [0-9] ", "", ids)) %>% 
          select(-ids) %>% 
          select(id, type, everything())
    df
    
    Source: local data frame [12 x 4]
    
          id  type values       date
       (chr) (chr)  (int)      (chr)
    1      8 type1      1 2015-08-08
    2      8 type1      2 2015-08-07
    3      8 type1      3 2015-08-06
    4      8 type2      4 2015-03-04
    5      8 type2      5 2015-03-03
    6      8 type2      6 2015-03-02
    7      9 type1      3 2015-05-03
    8      9 type1      4 2015-05-02
    9      9 type1      5 2015-05-01
    10     9 type2      2 2015-02-01
    11     9 type2      3 2015-01-31
    12     9 type2      4 2015-01-30
    

    【讨论】:

      【解决方案2】:

      我没有粘贴整个脚本来执行此操作,而只是主要步骤

      首先,对于每个数据框,通过拆分提取名称,并使用assignrbind为该数据框分配值。

      lapply(1:length(trying),function(x){
          newDFName <- strsplit(sub("^\\s+","",names(trying[x]))," ")[[1]][2]
          #use newDFName along with assign and rbind to assign the new data frame the rows
      })
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-02-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-04-30
        • 1970-01-01
        • 1970-01-01
        • 2017-12-28
        相关资源
        最近更新 更多