s-news
[Top] [All Lists]

Function to create multiple vectors and save them

To: s-news@wubios.wustl.edu
Subject: Function to create multiple vectors and save them
From: P.J.Wells@open.ac.uk
Date: Tue, 18 Jun 2002 18:41:34 +0100
I need to produce a series of samples and save each sample to a file, then
recall the samples and do things with them.

I've managed to write a script to produce the series of samples and save
them, but I'm having difficulty turning it into a function.

Specifically, the following code

        p <- rgamma(10,2)
        k <- runif(10)

        folder.name <- "D:\\Data sets\\F A M E\\Results\\"
        
        mkdir(paste(folder.name, deparse(substitute(p)), sep=""))
        
        n <- 100

        for (i in 1:n) {
                assign(paste("sample", i, sep=""), RS2.sample(p, k, 100))
                data.dump(paste("sample", i, sep=""), paste(folder.name,
deparse(substitute(p)), "\\sample", i, sep=""))
                }

works OK, and its results can be retrieved with

        fetch.RS2.samples <- function(samples, folder.name="D:\\Data sets\\F
A M E\\Results\\"){
                for (i in 1:100){
                data.restore(paste(folder.name,
deparse(substitute(samples)), "\\sample", i, sep=""))
                }
        
        sample.list <- vector(mode="list",length=100)

        for(i in 1:100){
                sample.list[[i]] <- get(paste("sample",i,sep=""))
                }
                
        sample.list
        }

        fetch.RS2.samples(p)

However

        RS2.meta.sample <- function(p, k, folder.name="D:\\Data sets\\F A M
E\\Results\\", n=100){
        
                mkdir(paste(folder.name, deparse(substitute(p)), sep=""))
        
                for (i in 1:n) {
                        assign(paste("sample", i, sep=""), RS2.sample(p, k,
100))
                        data.dump(paste("sample", i, sep=""),
paste(folder.name, deparse(substitute(p)), "\\sample", i, sep=""))
                        }
        }

        RS2.meta.sample(p, k)

produces files which the Windows Explorer reports as being of 0k size, and
for which using

        fetch.RS2.samples(p)

results in

        Error in get.default(paste("sample", i, sep = ..: Object "sample1"
not found


Evidently both I and the function are missing something, but what? 

Julian Wells
OU Business School
The Open University
Walton Hall
Milton Keynes
MK7 6AA
United Kingdom
+44 1908 654658


                


<Prev in Thread] Current Thread [Next in Thread>
  • Function to create multiple vectors and save them, P . J . Wells <=