在R中循环读取许多文件 [英] Loop in R to read many files

查看:104
本文介绍了在R中循环读取许多文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在想是否有人知道一种创建循环的方法,该循环可以在R中加载文件/数据库. 说我有一些这样的文件:data1.csv,data2.csv,...,data100.csv.

I have been wondering if anybody knows a way to create a loop that loads files/databases in R. Say i have some files like that: data1.csv, data2.csv,..., data100.csv.

在某些编程语言中,您可以执行类似数据+ {x} + .csv的操作,系统会像datax.csv一样识别它,然后可以应用循环.

In some programming languages you one can do something like this data +{ x }+ .csv the system recognizes it like datax.csv, and then you can apply the loop.

有什么想法吗?

推荐答案

Sys.glob()是另一种可能性-其唯一目的是遍历或通配符扩展.

Sys.glob() is another possibility - it's sole purpose is globbing or wildcard expansion.

dataFiles <- lapply(Sys.glob("data*.csv"), read.csv)

这会将所有格式为data[x].csv的文件读入列表dataFiles,其中[x]什么也不是.

That will read all the files of the form data[x].csv into list dataFiles, where [x] is nothing or anything.

[请注意,这是与@Joshua的答案不同的模式.在这里,list.files()使用正则表达式,而Sys.glob()仅使用标准通配符;可以使用哪些通配符取决于系统,可以在帮助页面?Sys.glob上找到可以使用的详细信息.]

[Note this is a different pattern to that in @Joshua's Answer. There, list.files() takes a regular expression, whereas Sys.glob() just uses standard wildcards; which wildcards can be used is system dependent, details can be used can be found on the help page ?Sys.glob.]

这篇关于在R中循环读取许多文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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