In R, I am trying to process multiple files using a loop. 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:

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

inside 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. The final output is a list of datasets.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top