cryptic0
cryptic0

Reputation: 245

How to write columns from multiple files into one data frame

I have around 100 text files each with 3 columns. I want to read every file into an object and then the contents to a matrix that has 300 columns in it.

Created a matrix:

ptamat <- matrix(ncol=300, nrow=2665)

Read files into an object

myfiles <- lapply(Sys.glob('pta_out__*'), read.table)

Show contents of first 2 files in the myfiles object

myfiles[[1:2]]

Copy files in the 'myfiles' object to the matrix

ptamat[,1:300] <- myfiles[[1:100]]

The last part does not work. Any ideas?

Upvotes: 3

Views: 118

Answers (1)

sebastian-c
sebastian-c

Reputation: 15415

The following should cbind all list elements:

do.call(cbind, myfiles)

Upvotes: 3

Related Questions