arun
2013-Nov-21 06:34 UTC
[R] how can I import a number of datsets in a folder in my working directory to a list in R
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.
arun
2013-Nov-21 14:06 UTC
[R] how can I import a number of datsets in a folder in my working directory to a list in R
Hi,
Try:
#Generating the files
fileList1 <- paste0(rep(LETTERS[1:5],each=3),1:3,".txt")
set.seed(48)
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)})
dir()
#[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)
D
# [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
?dim(res1)
#[1]?? 15 1686
#or
res2 <- do.call(rbind,lapply(D,function(x) {x1 <-
read.table(paste0(getwd(),"/IR/",x),header=TRUE);x1[,2]}))?
?identical(res1,res2)
#[1] TRUE
A.K.
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
auto.r
rangescale.r
mncn.r
hcluster.r
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:
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.