每个人。我想删除多个文件(CSV)中的某些列。 例如,我有50个文件。我想删除每个文件中的a,b,c列。
重点是我不知道如何获取文件。将更改保存在每个文件中,并保留原始文件名。
library(tidyverse)
# I want to delet some column which contain messy code
# input a list of file
df <- list.files(here("Data"),pattern=".csv",full.names = TRUE) %>%
lapply(read_csv) %>% #read csv
lapply(subset,select = -c(a,b,c)) #To remove the messy code
write.csv(df, file = here())
# I want to save the change in the original files, but I don't know how to do it.
答案 0 :(得分:0)
将所有文件(如果所有文件都在工作目录中)直接读取到列表中并进行处理。
files <- list.files() #if you want to read all the files in working directory
lst2 <- lapply(files, function(x) read.table(x, header=TRUE))
lapply(lst2,`[`,c(-a,-b,-c)