R:如何在多个csv中提取列,然后在一个文件夹中写入多个csv

我有一个包含多个csv的文件夹(文件夹1):“ x.csv”,“ y.csv”,“ z.csv” ... 我想提取每个文件的第3列,然后在新文件夹(文件夹2)中写入新的csv文件。因此,文件夹2必须包含“ x.csv”,“ y.csv”,“ z.csv” ...(但仅包含第三列)。

我尝试了这个:

dfiles <- list.files(pattern =".csv") #if you want to read all the files in working directory
lst2 <- lapply(dfiles, function(x) (read.csv(x, header=FALSE)[,3]))

但是我得到了这个错误:

 Error in `[.data.frame`(read.csv(x, header = FALSE), , 3) : 
  undefined columns selected 

而且,我不知道如何编写多个csv。

但是,如果我使用一个文件执行此操作,尽管输出位于同一文件夹中,它仍可以正常工作:

essai <-read.csv("x.csv", header = FALSE, sep = ",")[,3]
write.csv (essai, file = "x.csv")

任何帮助,将不胜感激。

评论
  • 时光逆转
    时光逆转 回复

    所以这就是我要怎么做。也许有更好,更有效的方法,但它仍然应该可以很好地工作。

    setwd("~/stackexchange") #set your main folder. Best way to do this is actually the here() package. But that's another topic.
    
    
    library(tools) #for file extension tinkering
    folder1 <- "folder1" #your original folder
    folder2 <- "folder2" #your new folder
    
    #I setup a function and loop over it with lapply.
    write_to <- function(file.name){
    file.name <-  paste0(tools::file_path_sans_ext(basename(file.name)), ".csv")
    essai <-read.csv(paste(folder1, file.name, sep = "/"), header = FALSE, sep = ",")[,3]
    write.csv(essai, file = paste(folder2, file.name, sep="/")) 
    }
    
    
    # get file names from folder 1
    dfiles <- list.files(path=folder1, pattern ="*.csv") #if you want to read all the csv files in folder1 directory
    
    lapply(X = paste(folder1, dfiles, sep="/"), write_to)
    

    Have fun! Btw: if you have many files, you could use data.table::fread and data.table::fwrite which improves csv reading by a lot.