【发布时间】: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>.
任何帮助
已编辑
我的文件看起来
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 <- sub('\\.csv$', '', basename(filenames))并尝试您的map2解决方案。 -
感谢您的工作,如何将列表转换为数据框?
-
现在我有了带有文件名的列名 id 但我不知道如何将这个大列表转换为数据框
-
使用
map2_df而不是map2。 -
谢谢 map2_df 给出了这个错误错误:不能合并
CHROM和 CHROM。
标签: r