[R] How to use For loop to read multiple files

arun smartpink111 at yahoo.com
Thu Nov 21 07:45:09 CET 2013



I forgot to delete the `sep` from the 2nd option:
 lapply(1:4,function(i) {paste0("file_",i,".txt")})
A.K.


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

I guess the trouble is here:
lapply(1:4,function(i) {paste(("file_",i,".txt"),sep="")})
Error: unexpected ',' in "lapply(1:4,function(i) {paste(("file_",

 lapply(1:4,function(i) {paste("file_",i,".txt",sep="")}) #works
#or
lapply(1:4,function(i) {paste0("file_",i,".txt",sep="")})
A.K.




Hi guys im having trouble running a for loop 

i have files name ie dog_1.txt to dog_70.txt which have been 
split into said files due to size issues and small memory on my computer 

## Getting correlation data 
sto = numeric(67) 

#Loop function to get data imported and obtain correlation 
for(i in 1:67) { 
  
  file = paste(("/Users/uqmbui/Dropbox/QBI/Data:Information/split_by_twin/data_mz_partial_normalized_", i, ".txt"), sep="") 
  d = read.table(file) 
  sto[i] = cor(d[,1], d[,2]) 
} 

The problem is the file naming because some how it keeps putting in characters outside the quotation marks???? 

this is the output: 

+ file = paste(("/Users/uqmbui/Dropbox/QBI/Data:Information/split_by_twin/data_mz_partial_normalized_", i, ".txt"), sep="") 
Error: unexpected ',' in: 
"for(i in 1:67) { 
file = paste(("/Users/uqmbui/Dropbox/QBI/Data:Information/split_by_twin/data_mz_partial_normalized_"," 

As you can see, it goes right ahead and reads outside the quotation marks and even skips the .txt part... 

Any help is appreciated



More information about the R-help mailing list