[R] creating a dynamic output vector

jim holtman jholtman at gmail.com
Thu Nov 8 04:25:00 CET 2007


Here is a script that will find all the atomic objects of length 1 and
put them in a dataframe that you then use to determine what variables
are there.

> a <- 1   # generate some atomic objects
> b <- 1.3
> x.char <- "character string"
> x.log <- TRUE
> x.real <- pi
> # get all atomic objects of length 1
> my.vars <- sapply(ls(), function(..x){
+     .val <- get(..x)
+     if (is.atomic(.val) && (length(.val) == 1)) return(.val)
+     else return(NULL)
+ })
> # delete the NULLs
> my.vars <- my.vars[!sapply(my.vars, is.null)]
> # dataframe with values
> str(do.call('data.frame', my.vars))
'data.frame':   1 obs. of  5 variables:
 $ a     : num 1
 $ b     : num 1.3
 $ x.char: Factor w/ 1 level "character string": 1
 $ x.log : logi TRUE
 $ x.real: num 3.14


On Nov 7, 2007 9:00 PM, Steve Powers <smpowers at wisc.edu> wrote:
> Everyone is assuming I know what the output data are, or that they come
> out from my model in some easily called vector. But I don't, and they do
> not. The outputs are hidden, and all are separate variables that need to
> be called. Also which ones come out after a given run will vary each
> time. All I have to reference the desired output variables are the names
> of every POTENTIAL output in character format, all in one vector.
>
> In any event, if anyone knows how to simply put all workspace variable
> names into one character vector, and their values/strings in another
> vector, that should work and I could take it from there (all I need are
> the one element variables, not vectors or data frames). Initially I
> messed around for awhile with ls() and ls.str() with no luck. Can't get
> those data into a useful format.
>
>
>
>
> jim holtman wrote:
> > Here is a function that might do what you want:
> >
> >
> >> # function to create the output
> >> f.output <- function(dat){
> >>
> > +     # create the base output vector
> > +     output.base <- rep(NA,10)
> > +     names(output.base) <- paste("var", 1:10, sep='')
> > +     output.base[names(dat)] <- dat
> > +     output.base
> > + }
> >
> >> f.output(c(var1=10, var5=5, var2=2))
> >>
> >  var1  var2  var3  var4  var5  var6  var7  var8  var9 var10
> >    10     2    NA    NA     5    NA    NA    NA    NA    NA
> >
> >> f.output(c(var6=6, var10=10, var1=1))
> >>
> >  var1  var2  var3  var4  var5  var6  var7  var8  var9 var10
> >     1    NA    NA    NA    NA     6    NA    NA    NA    10
> >
> >> f.output(c(var4=4, var4=5))  # same variable with two values -- last one taken
> >>
> >  var1  var2  var3  var4  var5  var6  var7  var8  var9 var10
> >    NA    NA    NA     5    NA    NA    NA    NA    NA    NA
> >
> >
> >
> > On 11/7/07, Steve Powers <smpowers at wisc.edu> wrote:
> >
> >> Let's say I have a program that returns variables whose names may be any
> >> string within the vector
> >> NAMES=c("varA","varB","varC","varD","varE","varF"..."varZ"), but I do
> >> not ever know which ones have actually been created. So in one example
> >> output, "varA", "varC", and "varD" could exist, but in another example
> >> output "varA", "varD", "varE",and "varF" exist, with no pattern or
> >> predictability (different combinations can come out, as well as
> >> different numbers of variables).
> >>
> >> How do assign the output values, in pre-arranged order, into an output
> >> vector? The output vector for the first example would be OUTPUTS=c(varA,
> >> NA, varC, varD...) and the output vector for the second example would be
> >> OUTPUTS=c(varA, NA, NA, varD, varE, varF...).  In other words, the rows
> >> for all potential returned values need to be retained in the order set
> >> by NAMES, and the values all need to be plugged into their respective
> >> spot in that order if they exist. Otherwise NA is plugged in.
> >>
> >> One other factor is that some outputs are values, but others are text. Tips?
> >>
> >>
> >> Using R version 2.4 on Windows XP
> >>
> >> ______________________________________________
> >> R-help at r-project.org mailing list
> >> https://stat.ethz.ch/mailman/listinfo/r-help
> >> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> >> and provide commented, minimal, self-contained, reproducible code.
> >>
> >>
> >
> >
> >
>
>



-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem you are trying to solve?



More information about the R-help mailing list