Skip to content

Automatic file reading

6 messages · Anders Malmberg, Adaikalavan Ramasamy, Uwe Ligges +3 more

#
Hi,

I want to do automatic reading of a number of tables (files) stored in 
ascii format
without having to specify the variable name in R each time.  Below is an 
example
of how I would like to use it (I assume files pair1,...,pair8 exist in 
spec. dire.)

for (i in 1:8){
  name <- paste("pair",i,sep="")
  ? ? ? <- read.table(paste("/home/andersm/tmp/",name,sep=""))
}

after which I want to have pair1,...,pair8 as tables.

But I can not get it right. Anybody having a smart solution?

Best regards,
Anders Malmberg
#
for(i in 1:10){ assign( paste("data", i), i ) }
[1] 1
[1] 5
[1] 13

See help("assign") for more details and examples.
On Wed, 2004-11-24 at 11:10, Anders Malmberg wrote:
#
Anders Malmberg wrote:

            
pairlist <- vector(8, mode = "list")
for (i in 1:8){
   name <- paste("pair",i,sep="")
   pairlist[[i]] <- read.table(paste("/home/andersm/tmp/",name,sep=""))
}

or use assign(), but you don't want to do that really.

Uwe Ligges
#
Hi Andreas,

what's about:
pair <- list()
for (i in 1:8){
   name <- paste("pair",i,sep="")
   pair[[ i ]] <- read.table(paste("/home/andersm/tmp/",name,sep=""))
}

Arne
On Wednesday 24 November 2004 12:10, Anders Malmberg wrote:

  
    
#
If you simply want read all files in a given directory, you can do 
something like:

fullpath = "/home/andersm/tmp"
filenames <- dir(fullpath,pattern="*")
pair <- sapply(filenames,function(x) 
{read.table(paste(fullpath,'/',x,sep=""))})

Sorry, untested.  But the point is that you can use dir to get all of 
the filenames specified by pattern from a directory specified by 
fullpath.

Sean
On Nov 24, 2004, at 7:31 AM, Arne Henningsen wrote:

            
1 day later
#
Sean Davis wrote:
Slightly off-topic but it is more portable to use the file.path function 
instead of paste when creating a file name.