[英]R looping file import
Is there a way to loop this? 有办法循环吗?
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')
How would the code look like? 代码看起来如何?
ANSWER: 回答:
Here is a good example of how you would automatically import multiple files at once: 这是一个很好的示例,说明如何一次自动导入多个文件:
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")
Try this out: 试试看:
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]]))
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.