【问题标题】:add a counter to lapply(do.call) - R添加一个计数器 lapply(do.call) - R
【发布时间】:2019-10-12 21:49:06
【问题描述】:

我有一个包含许多文件夹的文件。我编写了以下代码来获取所有文件的地址,将它们导入 R 并绑定它们,效果很好。问题是,某些文件的列数不同,导致显示错误。我的问题是如何在我的代码的第三行添加一个计数器?我基本上想检查我的计数器何时停止,然后我手动删除与其他列数不同的文件。 谢谢

file_names <- list.files(path="D:/ABCDE", recursive=TRUE)
setwd("D:/ ABCDE ") 
all_dta <- do.call(rbind, lapply(file_names, function(x) read.csv(file=x,header = FALSE))) 

【问题讨论】:

  • 您可以使用 data.table::rbind 和 fill = TRUE 用 NA 填充缺失的列

标签: r counter lapply rbind do.call


【解决方案1】:

要为lapply 添加一个计数器,我会这样做:

file_names <- list.files(path="D:/ABCDE", recursive=TRUE)
idx=1:length(filenames) #this will server as your 'counter'
lapply(idx, function(i) {print(file_names[i]); read.csv(file=file_names[i],header = FALSE)}) # this will print the file and when the loop stops you'll see the file that is faulty

但是,作为另一种解决方案 - 既要知道哪些文件有问题并自然地跳过它们 - 我会这样做:

wanted=c()
for(f in file_names){
    first_line=system(paste0('head -n 1',f),intern=T) # sends prompt to command line to print first line of files. intern=T means one can set this to a variable
if(nchar(first_line > quota)){ #set quota to provide threshold for a number of columns
     wanted=c(wanted,f)
}
}

您也可以使用sapply 执行上述操作。

然后:

all_dta <- do.call(rbind, lapply(wanted, function(x) read.csv(file=x,header = FALSE))) 

如果您想知道哪些文件有问题(即哪些文件不包含足够的列)。只需找到列数少于配额的文件即可:

unwanted=c()
for(f in file_names){
    first_line=system(paste0('head -n 1',f),intern=T) # sends prompt to command line to print first line of files. intern=T means one can set this to a variable
if(nchar(first_line < quota)){ 
     unwanted=c(unwanted,f)
}
}

【讨论】:

    【解决方案2】:

    一个更简单的解决方案是修改调用以跳过第一行并使用 dplyr::bind_rows() 获取所有文件

    file_names <- list.files(path="D:/ABCDE", recursive=TRUE)
    setwd("D:/ ABCDE ") 
    all_dta <- do.call(dplyr::bind_rows, lapply(file_names, function(x) read.table(file=x,header = FALSE, sep = ',', skip = 1)))
    

    唯一的事情是您需要设置列名。如果没有很多列,您可以读取一行来获取名称或手动执行。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-06-03
      • 1970-01-01
      • 2015-07-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多