我有数千个扩展名为.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
)为每个文件。我这样做对吗?如果你知道每一个步骤,请帮忙。
先谢谢你
你可以使用.NET Framework 2.0来实现。
library(dplyr)
columnNames <- c("year", "month", "day", "weather")
FilesList <- list.files(workingdirectory, pattern = "\\.txt$", full.names = TRUE)
purrr::map(FilesList, ~{
.x %>%
#Read csv file
read.csv(sep = "", header = FALSE, stringsAsFactors = FALSE) %>%
#Remove Last column
select(-ncol(.)) %>%
#Rename at particular position with columnNames
rename_at(c(1,2,3,7), ~columnNames) %>%
#Remove 29th Februaury
filter(month != 2 & day != 29) %>%
#Write the data back
write.csv(paste0('after', basename(.x)), row.names = FALSE)
})
除非你有很强的理由将数据写入文本文件,否则我建议将数据写入csv中。