【问题标题】:Adding file name column to table as multiple files are read and merged在读取和合并多个文件时将文件名列添加到表中
【发布时间】:2020-07-26 05:27:59
【问题描述】:

我在一个文件夹中有多个列名相同的 .csv 文件

我想合并它们并将每个文件的名称添加为第一列

我试过了

filenames <- list.files(getwd(), full.names = FALSE, pattern = ".csv", recursive = TRUE)
sites <- str_extract(filenames, ".csv")  # same length as filenames

library(purrr)
library(dplyr)
library(readr)
stopifnot(length(filenames)==length(sites))  # returns error if not the same length
ans <- map2(filenames, sites, ~read_csv(.x) %>% mutate(id = .y))  # .x is element in filenames, and .y is element in sites

通过这个文件被加入,但第一列是数字而不是文件名

喜欢

> ans
[[1]]
# A tibble: 30,675 x 15
      X1 CHROM    POS REF   ALT   NORMAL TUMOR Depth T_REF_COUNT T_ALT_COUNT N_REF_COUNT
   <dbl> <chr>  <dbl> <chr> <chr> <chr>  <chr> <dbl>       <dbl>       <dbl>       <dbl>
 1     1 M     1.04e4 G     A     3235:… 6860…  6874        1188        5646        3216
 2     2 M     1.48e4 G     A     3147:… 6504…  6584        6249         210        3128

我也试过了,但说 CHROM 列是双列(数字和字符)

customized_read_csv <- function(file){
    read_csv(file) %>%
        mutate(fileName = file)
}

list.files(full.names = TRUE) %>% # list all the files
    lapply(customized_read_csv) %>% # read them all in with our custom function
    reduce(bind_rows) %>% # stack them all on top of each other
    select(CHROM, fileName, N_VAF) %>% # select the correct columns
    pivot_wider(names_from = fileName, values_from = N_VAF) # and switch from "long format" to "wide format"

Error: Can't combine `..1$CHROM` <character> and `..13$CHROM` <double>.

任何帮助

已编辑

我的文件看起来

my file

 ans <- map2_df(filenames, sites, ~read_csv(.x) %>% 
    +                    mutate(CHROM = as.character(CHROM), id = .y))
    Error: Mapped vectors must have consistent lengths:
    * `.x` has length 40
    * `.y` has length 38

最终文件看起来

> head(ans)
# A tibble: 6 x 10
     X1 CHROM     POS REF   ALT   T_ALT_COUNT N_REF_COUNT N_ALT_COUNT id     T_REF_COUNT
  <dbl> <chr>   <dbl> <chr> <chr>       <dbl>       <dbl>       <dbl> <chr>        <dbl>
1     1 chrM     8620 C     A            2161         607           1 LP200…          NA
2     2 chr1   983023 C     T               9          31           0 LP200…          NA
3     3 chr1  1205584 T     A               9          23           0 LP200…          NA
4     4 chr1  1495120 T     G               7          29           0 LP200…          NA
5     5 chr1  1772044 C     T              16          39           1 LP200…          NA
6     6 chr1  2302194 G     T              10          20           0 LP200…          NA
> 

请问如何避免这种情况?

【问题讨论】:

  • sites 替换为sites &lt;- sub('\\.csv$', '', basename(filenames)) 并尝试您的map2 解决方案。
  • 感谢您的工作,如何将列表转换为数据框?
  • 现在我有了带有文件名的列名 id 但我不知道如何将这个大列表转换为数据框
  • 使用map2_df 而不是map2
  • 谢谢 map2_df 给出了这个错误错误:不能合并CHROM CHROM

标签: r


【解决方案1】:

您可以使用sub 来获取文件名。似乎CHROM 列在某些文件中被读取为数字,我们可以将其显式转换为字符。试试看:

library(dplyr)
library(purrr)

sites <- sub('\\.csv$', '', basename(filenames))

ans <- map2_df(filenames, sites, ~read_csv(.x) %>% 
                                   mutate(CHROM = as.character(CHROM), id = .y))

【讨论】:

  • 抱歉@Ronak Shah 在我的新数据中,因为我在每个文件的第一列中有一个空列,在最终统一文件中我有一个空列,请查看我编辑的帖子
  • 你能问这个作为一个新问题吗?扩展问题可能会让未来的读者感到困惑。
  • 谢谢我作为一个新问题问了,请看看stackoverflow.com/questions/63133306/…
猜你喜欢
  • 2018-02-28
  • 1970-01-01
  • 1970-01-01
  • 2017-08-03
  • 2021-11-26
  • 2020-03-12
  • 2021-06-13
  • 1970-01-01
相关资源
最近更新 更多