[R] Automating object creation

baptiste auguie ba208 at exeter.ac.uk
Tue Apr 14 19:49:29 CEST 2009


?assign

(but are you sure you really want to name all these objects  
separately? Usually in R you would put them together in a list or a  
data.frame, it is much more convenient for later manipulations)

On 14 Apr 2009, at 18:32, Zachary Patterson wrote:

> I am new to R. I would like to automate the creation of a number of
> vectors but can't seem to get the string formatting to work.
>
> Here's what I would like to be able to do:
>
> Suppose we have a vector:
> x <- c(2,4,5)
>
> I would like to be able to create a set of vectors whose names are
> associated with the values in x - e.g.
>
> x2 <- 0
> x4 <- 0
> x5 <- 0
>
> I have tried with a for loop and eval and sprintf, paste, etc. but end
> up with the following error:
>
> Error in sprintf("%s%i", "x", 1) <- 0 :
>  target of assignment expands to non-language object
>
> How can I assign a string formatted name to a vector?
>
> Any help appreciated,
> Zak
>
> ______________________________________________
> 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.

_____________________________

Baptiste Auguié

School of Physics
University of Exeter
Stocker Road,
Exeter, Devon,
EX4 4QL, UK

Phone: +44 1392 264187

http://newton.ex.ac.uk/research/emag




More information about the R-help mailing list