R语言:批量导入文件夹中的文件

本文以csv格式为例,总结了以下几种方法:

1、
temp = list.files(pattern="*.csv")
for (i in 1:length(temp)) assign(temp[i], read.csv(temp[i]))
temp = list.files(pattern="*.csv")
list2env(lapply(setNames(temp, make.names(gsub("*.csv$", "",temp))), read.csv), envir = .GlobalEnv)
2files = list.files(pattern="*.csv")
myfiles = do.call(rbind, lapply(files, function(x) read.csv(x, stringsAsFactors = FALSE)))
3、
cat *.csv > all.csv
cat *.csv | grep -v ^Age > all.csv
4、
path <- "C:/Users/cfees/My Box Files/Fitness/"
files <- list.files(path=path, pattern="*.csv")
for(file in files)
{
  perpos <- which(strsplit(file, "")[[1]]==".")
  assign(
    gsub(" ","",substr(file, 1, perpos-1)), 
    read.csv(paste(path,file,sep="")))
}
5for(f in 1:length(List_of_filepaths)) {
  file_name <- str_sub(string = List_of_filepaths[f], start = 46, end = -5)
  file_df <- read_csv(List_of_filepaths[f])  
  file_df <- file_df[,1:3] #if you only need the first three columns
  assign( x = file_name, value = file_df, envir = .GlobalEnv)
}
6、
temp = list.files(pattern=".csv$") #list all the files in dir that have .csv at the end
for (i in 1:length(temp)) {
  assign(temp[i], prettify(toJSON(read.csv(temp[i])))) #read each csv, convert to  JSON,make pretty, give it a name
  write(get(temp[i]),paste(temp[i],"json", sep=".")) #write the json to a file
}
7、
xlist<-list.files(pattern = "*.csv")
for(i in xlist) { 
  x <- read.csv((i))
  assign(i, x)
}
8、
temp <- list.files(pattern="*.csv")
for (i in 1:length(temp)) {
  temp2 = read.csv(temp[i], header = TRUE)
  x <- rbind(x,temp2)
}
  • 7
    点赞
  • 51
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值