我有成千上万个扩展名为.txt的文件,并在公共文件夹中使用空格(“”)作为分隔符。我需要:
# Setting working directory
workingdirectory <- "D:/FolderContainsThousandsFile"
setwd(workingdirectory)
# Listing the files in the folder with .txt extension
FilesList <- list.files(workingdirectory, pattern = ".txt$")
numberFiles <- length(FilesList)
# Looping for all files
for(f in 1:numberFiles){
# read the file into tables
FilterFile <- FilesList [f] %>% read.csv(sep = "", header = FALSE, stringsAsFactors = FALSE) %>% dplyr::select(-ncol(.)) # remove the last column
# Remove the 29th day in February
columnNames <- c("year", "month", "day", "weather")
FilterFile <- FilterFile %>% rename_at(c(1,2,3,7), ~columnNames) # renaming columns to indicate the column to be taken
FilterFile <- FilterFile %>% filter(month != 2 | day != 29)
before_file1.txt
转换为after_file1.txt
)的每个文件。我做对了吗?如果您知道执行此操作的每个步骤,请提供帮助。
提前谢谢您
正如托马斯·罗莎(Thomas Rosa)所指出的,到目前为止,最好有更多有关您的文件,目标和方法的详细信息...
但是,您可能正在寻找的代码可能与此类似:
cl <- c(1, 3, 5, 7) # Columns you want
rw <- c(2, 5) # Rows you do not want
files <- list.files(path = 'your directory') # List the thousands of files
for(file in files) {
temporal <- read.table(file = file, sep = "") # You may want to skip
# some rows or include headers
temporal <- temporal[, cl] # Just use the columns you want
temporal <- temporal[-rw, ] # Delete the undesired rows
write.table(x = temporal, file = paste0('after_', file))
}
注意:我不建议您在文件名中使用点,因为如果您的操作系统认为它是扩展文件指示符,则可能会遇到麻烦。