在多个文件上使用lapply和read.csv(在R中) [英] Using lapply and read.csv on multiple files (in R)

查看:88
本文介绍了在多个文件上使用lapply和read.csv(在R中)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想这是一个初学者的问题,但是我还没有找到答案或弄清楚我在做什么错.

I guess this is a bit of a beginner's question but I haven't quite found an answer or figured out what I'm doing wrong.

我正在尝试使用以下方法读取20个CSV文件,这些文件存储在单独的目录中:

I'm trying to read 20 CSV files that are stored in a separate directory using:

setwd("./Data")
filenames <- list.files()  
All <- lapply(filenames,function(i){
  i <- paste(".\\",i,sep="")
  read.csv(i, header=TRUE, skip=4)
})

然后出现以下错误:

Error in file(file, "rt") : cannot open the connection
In addition: Warning message:
In file(file, "rt") :
  cannot open file '.\filename.csv': No such file or directory

文件名代表我文件夹中第一个文件的名称.

Where filename stand for the name of the first file in my folder.

提前谢谢!

推荐答案

尝试仅删除以下内容:i <- paste(".\\",i,sep="")

Try just removing the: i <- paste(".\\",i,sep="")

read.csv应该可以与list.files(full.names=TRUE)输出正常工作

read.csv should work fine with the list.files(full.names=TRUE) output

setwd("./Data")
filenames <- list.files(full.names=TRUE)  
All <- lapply(filenames,function(i){
  read.csv(i, header=TRUE, skip=4)
})

这篇关于在多个文件上使用lapply和read.csv(在R中)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆