简体   繁体   English

读取R中的文件列表

[英]reading list of files in R

In R, I am trying to process multiple files using a loop. 在R中,我试图使用循环处理多个文件。 A file contains paths of many other files that contain data for computation. 文件包含许多其他文件的路径,这些文件包含用于计算的数据。 Can somebody tell me the simple script to do it? 有人可以告诉我这个简单的脚本吗?

list.txt: LIST.TXT:

/data/tmp/b.dat
/data/tmp/c.dat
/data/tmp/d.dat

inside b.dat: 在b.dat内:

1 15.30 20.30 15.0
2 7.3 5.0 2.0
...
n 5.0 2.0 6.0

In the meantime, data is processed for each row. 同时,处理每行的数据。 How can the data be transposed? 如何转置数据?

Here's a one-liner: 这是一个单行:

lapply(scan("list.txt",""), read.table,sep=" ", row.names=1)

A short explanation: scan("list.txt","") scans your file containing the list of locations (space separated)and outputs them as a vector of characters (because of the "" ) then with lapply you apply to each of those locations the function read.table with the right separator and other needed arguments. 简短说明: scan("list.txt","")扫描包含位置列表(空格分隔)的文件,并将它们输出为字符向量(因为"" )然后用lapply应用于每个这些位置函数read.table与正确的分隔符和其他所需的参数。 The final output is a list of datasets. 最终输出是数据集列表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM