Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write columns from multiple files into one data frame

Tags:

r

lapply

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?

like image 669
cryptic0 Avatar asked Dec 05 '25 08:12

cryptic0


1 Answers

The following should cbind all list elements:

do.call(cbind, myfiles)
like image 166
sebastian-c Avatar answered Dec 07 '25 20:12

sebastian-c



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!