[R] Automating object creation
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@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.
Re: [R] Automating object creation
?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@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 __ R-help@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.
Re: [R] Automating object creation
On Tue, Apr 14, 2009 at 6:32 PM, Zachary Patterson zak.patter...@gmail.com 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? Ooh, you're so close. Read the help page for assign perhaps? help(assign) and hence enlightenment. Also note it's not normally a good thing to do. Better to stick these things in a list, because your next question is going to be 'how do I **get** the value of x1, x2, x3 etc in a loop if i is 1,2 or 3?'. The answer to that one is 'Read the help page for get'. But if you stick your stuff in a list object the answer is simply x[[i]]. See also FAQ 7.21: http://cran.r-project.org/doc/FAQ/R-FAQ.html#How-can-I-turn-a-string-into-a-variable_003f But really, use a list :) Barry __ R-help@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.
Re: [R] Automating object creation
It is certainly possible to create x2, x4, etc. using something like assign( sprintf(x%d,i), ...value... ). But are you sure you need separate *variables* x2, x4, etc.? Why not create a list of vectors addressible as x[2] etc.? You can do that with x - list() (to define the data type of x as allowing generic objects) then x[2] - ... value ... etc. -s On Tue, Apr 14, 2009 at 1:32 PM, Zachary Patterson zak.patter...@gmail.com 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@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. __ R-help@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.
Re: [R] Automating object creation
Wow! I wasn't expecting such quick and helpful responses. I will see which of these suggestions works best for what I am doing. Thanks very much to all of you. Zak On Tue, Apr 14, 2009 at 1:58 PM, Stavros Macrakis macra...@alum.mit.edu wrote: It is certainly possible to create x2, x4, etc. using something like assign( sprintf(x%d,i), ...value... ). But are you sure you need separate *variables* x2, x4, etc.? Why not create a list of vectors addressible as x[2] etc.? You can do that with x - list() (to define the data type of x as allowing generic objects) then x[2] - ... value ... etc. -s On Tue, Apr 14, 2009 at 1:32 PM, Zachary Patterson zak.patter...@gmail.com 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@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. __ R-help@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.