将列表名称附加到data.frames列中

时间:2018-05-14 14:34:01

标签: r purrr

我导入了data.frames in1

列表
in1 <- file_paths [1:3] %>% 
  set_names(basename(.) %>% 
              gsub("_R1_001.bg.gz.bismark.cov.gz", "", .) %>% 
              gsub("CpG_", "", .)) %>% 
  map(readr::read_tsv, n_max = 3, 
      col_names = c("chr", "start", "end", "score", "ref", "alt"))

in1(少量文件,几行)的一个小例子如下所示:

$`OB_0120-53T_S26`
# A tibble: 3 x 6
  chr   start   end score   ref   alt
  <chr> <int> <int> <int> <int> <int>
1 chr1  19190 19190   100     1     0
2 chr1  19249 19249   100     1     0
3 chr1  19274 19274   100     1     0

$`OB_0120-55T_S25`
# A tibble: 3 x 6
  chr   start   end score   ref   alt
  <chr> <int> <int> <int> <int> <int>
1 chr1  17479 17479   100     2     0
2 chr1  17484 17484   100     2     0
3 chr1  17493 17493   100     2     0

$`OB_0120-56T_S36`
# A tibble: 3 x 6
  chr   start   end score   ref   alt
  <chr> <int> <int> <int> <int> <int>
1 chr1  17479 17479   100     1     0
2 chr1  17484 17484   100     1     0
3 chr1  17493 17493   100     1     0

我的目标是将它们加入一个更大的数据框架中,例如。

%>% reduce(full_join, by= c("chr", "start", "end"))

但首先我要将每个元素的列表名称附加到&#34;得分&#34; &#34; REF&#34;和&#34; alt&#34;列名。

因此,所需的第一个元素应如下所示......

$`OB_0120-53T_S26`
    # A tibble: 3 x 6
      chr   start   end OB_0120-53T_S26.score   OB_0120-53T_S26.ref   OB_0120-53T_S26.alt
      <chr> <int> <int> <int> <int> <int>
    1 chr1  19190 19190   100     1     0
    2 chr1  19249 19249   100     1     0
    3 chr1  19274 19274   100     1     0

我尝试过使用set_names,但我不知道如何获取列表元素的单数名称我得到名称的整个向量,因此这是错误的。

in1 %>% map(set_names, c("chr", "start", "end", paste0(names(.), c(".score", ".ref", ".alt"))))

$`OB_0120-53T_S26`
# A tibble: 3 x 6
  chr   start   end `OB_0120-53T_S26.score` `OB_0120-55T_S25.ref` `OB_0120-56T_S36.alt`
  <chr> <int> <int>                   <int>                 <int>                 <int>
1 chr1  19190 19190                     100                     1                     0
2 chr1  19249 19249                     100                     1                     0
3 chr1  19274 19274                     100                     1                     0

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

......实际上这是有效的

  in1 %>% 
      map2( ., names(.), 
      ~set_names(.x, c("chr", "start", "end", paste0(.y, c(".score", ".ref", ".alt")))))

$`OB_0120-53T_S26`
# A tibble: 3 x 6
  chr   start   end `OB_0120-53T_S26.score` `OB_0120-53T_S26.ref` `OB_0120-53T_S26.alt`
  <chr> <int> <int>                   <int>                 <int>                 <int>
1 chr1  19190 19190                     100                     1                     0
2 chr1  19249 19249                     100                     1                     0
3 chr1  19274 19274                     100                     1                     0

$`OB_0120-55T_S25`
# A tibble: 3 x 6
  chr   start   end `OB_0120-55T_S25.score` `OB_0120-55T_S25.ref` `OB_0120-55T_S25.alt`
  <chr> <int> <int>                   <int>                 <int>                 <int>
1 chr1  17479 17479                     100                     2                     0
2 chr1  17484 17484                     100                     2                     0
3 chr1  17493 17493                     100                     2                     0