문제

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