[R] how can I import a number of datsets in a folder in my working directory to a list in R

arun smartpink111 at yahoo.com
Thu Nov 21 07:34:10 CET 2013


Hi,

Suppose, if I create 15 files in my working directory.
set.seed(48)
lapply(1:15,function(i) {m1 <- matrix(sample(1:20,1686*2,replace=TRUE),nrow=1686,ncol=2); write.table(m1,paste0("file_",i,".txt"),row.names=FALSE,quote=FALSE)})

 D <-dir()
D1 <- D[order(as.numeric(gsub("\\D+","",D)))]
D1

 res <- t(sapply(D1,function(x) {x1<- read.table(x,header=TRUE); x1[,2]}))
dim(res)
#[1]   15 1686
#or
res1 <- do.call(rbind,lapply(D1,function(x) {x1<- read.table(x,header=TRUE); x1[,2]}))
 dim(res1)
#[1]   15 1686
 dimnames(res) <- dimnames(res1)
 identical(res,res1)
#[1] TRUE

A.K.


I have a folder containing 15 text files in my working directory.  I 
want to use the dir() function 
D<-dir(path="IR",all.files=F,full.names=F,recursive=T) to get the 
files in a filelist in R 
....D<-dir(path="IR",all.files=F,full.names=F,recursive=T) . the 
output is that D is a list of the names of the 15. however, the files 
are inaccessible. I want R to access to access each file which is a 
matrix of dimension 1686 by 2 so I can be able to form a new matrix (15 
by 1686) using the rbind function binding the second columns of the 15 
files together.



More information about the R-help mailing list