循环在文件名中包含模式的read.csv文件 [英] Loop read.csv files containing pattern in file name

查看:154
本文介绍了循环在文件名中包含模式的read.csv文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个包含30个单词的向量,称为俱乐部"

I created a vector with 30 words, called "club"

club <- pixid$ack1

接下来,我要导入30个csv文件.每个文件名包含"club"中的1个单词.

Next i want to import 30 csv files. Each filename contains 1 of the words in "club".

for (i in club){
DCM.[i] <- read.csv(list.files(pattern = "[i]"))
}

但是我收到以下错误:

文件(文件,"rt")中的错误:无效的描述"参数.

Error in file(file, "rt") : invalid 'description' argument.

如何读取所有包含矢量名称的文件?我希望这只是语法错误.

How can I read in all of these files containing names from the vector? I'm hoping this is just a syntax error.

推荐答案

俱乐部" vector中单个pattern可能有多个文件.我们遍历模式的俱乐部",使用list.files列出基于该模式的文件,然后遍历文件名,并使用read.csv

It is possible that there are multiple files for a single pattern in the 'club' vector. We loop through the 'club' of patterns, list the files based on that pattern using list.files and then loop through the file names, and read it with read.csv

DCM <- lapply(club, function(x) lapply(list.files(pattern = x), 
               function(x) read.csv(x, stringsAsFactors=FALSE, row.names = NULL)))

上面是嵌套的list,其中包含俱乐部"提供的每个图案的listdata.frame个.

The above is a nested list containing a list of data.frames for each pattern provided by the 'club'.

这篇关于循环在文件名中包含模式的read.csv文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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