[英]Importing and reading multiple files R
I have numerous tab delimited .txtfiles named as " abcd001.txt, abcd002.txt" .... stored in a directory. 我有许多制表符分隔的.txt文件,名为“ abcd001.txt,abcd002.txt” ....存储在一个目录中。 I was able to import them using the following code, (whereby default directory is sames as data files directory).
我能够使用以下代码导入它们(默认目录与数据文件目录相同)。 Its three column, all numeric type data
它的三列,所有数字类型的数据
filenames <- list.files(path=".",pattern="abcd+.*txt")
#list of data in R
names <-substr(filenames,1,6)
for(i in names){
filepath <- file.path(".",paste(i,".txt",sep=","))
assign(i, read.table(filepath,
colClasses=c("numeric"),
sep = "\t"))
}
The code itself hasnt returned any error. 代码本身没有返回任何错误。 My doubt is How can I access data which is being loaded?
我的疑问是如何访问正在加载的数据? How to access say the data of the file abcd011.txt which should be three column data
如何访问说文件abcd011.txt的数据应该是三列数据
The commands:names[3] just returns the file number 000002 But no data. 命令:names [3]只返回文件号000002但没有数据。
The code here is similar to one here: Read multiple CSV files into separate data frames . 此处的代码与此处的代码类似:将多个CSV文件读取到单独的数据框中 。
I would recommend to put the results of read.table
either in a list, or in one bit data.frame. 我建议将
read.table
的结果放在一个列表中,或者放在一个data.frame中。 In addition, I would recommend using apply
style loops here, either standard R ( lapply
) or plyr
. 另外,我建议在这里使用
apply
style loop,标准R( lapply
)或plyr
。 I prefer using plyr
, so my examples will be using that package. 我更喜欢使用
plyr
,所以我的例子将使用该包。 An example: 一个例子:
## Read into a list of files:
filenames <- list.files(path=".",pattern="abcd+.*txt")
list_of_data = llply(filenames, read.table,
colClasses = c("numeric"), sep = "\t")
Accessing the data can now be done using: 现在可以使用以下方式访问数据:
list_of_data[[1]]
list_of_data[["abcd1.txt"]]
Or you could read the contents of the files into one big data.frame
: 或者您可以将文件的内容读入一个大数据
data.frame
:
big_data = ldply(filenames, read.table,
colClasses = c("numeric"), sep = "\t"))
Accessing the contents of a file can now be done using: 现在可以使用以下命令访问文件的内容:
big_data[big_data$variable == "abcd1.txt",]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.