我试图通过50+ csv文件运行一大块代码,但我不知道如何做到这一点。所有的csv文件都有相同的列,但行数不同。
到目前为止,我有以下内容:
filelist<- list.files(pattern = ".csv") #made a file list with all the .csv files in the directory
samples <- lapply(filelist, function(x) read.table(x, header=T)[,c(1,2,3,5)]) #read all the csv files (only the columns I'm interested in)
output <- samples %>%
rename (orf = protein) %>%
filter (!grepl("sp", orf)) %>%
write.csv (paste0("new_", filename))
#I want to rename a column and remove all rows containing "sp" in that column, then export the dataframe as new_originalfilename.csv
任何帮助都将非常感激!
您可以在同一个lapply
循环中执行此操作。
library(dplyr)
lapply(filelist, function(x) {
read.table(x, header=T) %>%
select(1, 2, 3, 5) %>%
rename(orf = protein) %>%
filter(!grepl("sp", orf)) %>%
write.csv(paste0("new_", x), row.names = FALSE)
})