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
Steve
Is this the sort of thing you mean?
output <- character(26)
names(output) <- paste('var', LETTERS[1:26], sep='')
output
output[paste('var', LETTERS[c(2,4,6,7,16)], sep='')] <- c(1,
pi,
letters[1:3])
output
Peter Alspach
> -----Original Message-----
> From: r-help-bounces at r-project.org
> [mailto:r-help-bounces at r-project.org] On Behalf Of Steve Powers
> Sent: Thursday, 8 November 2007 11:27 a.m.
> To: r-help at r-project.org
> Subject: [R] creating a dynamic output vector
>
> 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.
>
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 takenvar1 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?