有办法循环吗?
jan2018 <- read.csv(file = 'Jan2018.csv')
feb2018 <- read.csv(file = 'Feb2018.csv')
mar2018 <- read.csv(file = 'Mar2018.csv')
apr2018 <- read.csv(file = 'Apr2018.csv')
may2018 <- read.csv(file = 'May2018.csv')
jun2018 <- read.csv(file = 'Jun2018.csv')
jul2018 <- read.csv(file = 'Jul2018.csv')
aug2018 <- read.csv(file = 'Aug2018.csv')
sep2018 <- read.csv(file = 'Sep2018.csv')
代码看起来如何?
答案:
下面是一个很好的示例,说明如何一次自动导入多个文件:
list_of_files <- list.files(pattern = "^starting_text")
data <- list_of_files %>%
setNames(nm = c("jan", "feb", "mar", "apr", "may", "jun", "jul")) %>%
map_df(read_xlsx, .id = "month")
答案 0 :(得分:1)
尝试一下:
file_names <- list.files(pattern = ".*2018.csv")
dates <- tolower(sub(".csv", "", file_names))
myList <- lapply(file_names, read.csv)
for (i in 1:length(myList)) {
assign(paste0(dates[i]), as.data.frame(myList[[i]]))
}