按id加入data.frames,其中id是数据框的名称,并将这些ID保存在行中

时间:2015-09-08 12:45:37

标签: r dataframe

我有一个data.frames列表,其中data.frames的名称包含ID以及'分组'特性。我想加入共享'特征'的数据框架。名称的一部分,并将id保存为此新data.frame上的行ID。这很难说,这是一个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.frames的列表,其中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")))

请注意,我的实际数据远大于此数据(更多行,更多数据框架和更多列),实际名称不是&#39; typeX&#39;但一般结构优先'space INT space TEXT' :(&#34; 8 type1&#34;)。因此,任何易于扩展的解决方案都是可取的。

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

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

首先,对于每个数据框,通过拆分名称来提取名称,然后使用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
})