[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 21:25:19 CET 2013

1st part:

res3 <- vector()
for(i in 1:length(D)){
 res3 <- rbind(res3, read.table(paste0(getwd(),"/IR/",D[i]),header=TRUE)[,2])
#[1]   15 1686
#[1] TRUE

2nd part:

vec1 <- unlist(lapply(D,function(x) {x1 <- read.table(paste0(getwd(),"/IR/",x),header=TRUE);x1[,1]}))
#[1] 25290
#[1] 25290

3rd part (not clear):
Dvec <- gsub("\\..*","",D)
# [1] "A1" "A2" "A3" "B1" "B2" "B3" "C1" "C2" "C3" "D1" "D2" "D3" "E1" "E2" "E3"


hi AK
 thanks . that was perfect! please i need to use 
the for-loop function (for i in 1:length(D)) to achieve same result. i 
also would like to be able to access the temporary matrix x1 so I can 
extract the first column(which is common to the 15 files) in a vector. 
how do I get the file names(i.e A1 A2 etc) in a character vector too? 
sorry to bother you AK, just that as a new R user, I am finding it a bit
 difficult. thanks in advance.

On Thursday, November 21, 2013 9:06 AM, arun <smartpink111 at yahoo.com> wrote:
#Generating the files

fileList1 <- paste0(rep(LETTERS[1:5],each=3),1:3,".txt")
lapply(fileList1,function(x) {m1 <- matrix(sample(1:20,1686*2,replace=TRUE),nrow=1686,ncol=2); write.table(m1,paste0("/home/arunksa111/Trial6/IR/",x),row.names=FALSE,quote=FALSE)}) 

#[1] "hcluster.r" "IR"     ##created another file in working directory to mimic the situation  
 D <- dir(path="IR",all.files=F,full.names=F,recursive=T)
# [1] "A1.txt" "A2.txt" "A3.txt" "B1.txt" "B2.txt" "B3.txt" "C1.txt" "C2.txt"
# [9] "C3.txt" "D1.txt" "D2.txt" "D3.txt" "E1.txt" "E2.txt" "E3.txt"

res1 <- do.call(rbind,lapply(D,function(x) {x1 <- read.table(paste0("/home/arunksa111/Trial6/IR/",x),header=TRUE);x1[,2]}))  ##change the path 
#[1]   15 1686
res2 <- do.call(rbind,lapply(D,function(x) {x1 <- read.table(paste0(getwd(),"/IR/",x),header=TRUE);x1[,2]}))  
#[1] TRUE


Thanks for your prompt reply AK. I tried the script and it is still not working. The situation is this: the 15 files are inside a 
folder named "IR" in my working directory. The folder is NOT my working 
directory but is INSIDE my working directory;so my working directory is 
just like this
IR(the folder that contains the 15 files)

The 15 files 
in the folder are named 
A1.txt,A2.txt,A3.txt,B1.txt,B2.txt,B3.txt,C1.txt,C2.txt,C3.txt,D1.txt,D2.txt,D3.txt,E1.txt,E2.txt,E3.txt. The challenge is to get the files into R using the dir() ,for loop 
,read.table and rbind to form a matrix containing only the 2nd column of each files. then create a vector of the filenames. If I make the folder "IR" my working directory, I wont be able to use the functions listed 
above on the data. Please help me look at this, critically. Thank you

On Thursday, November 21, 2013 1:34 AM, arun <smartpink111 at yahoo.com> wrote:

Suppose, if I create 15 files in my working directory.
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)))]

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


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