[R] using variable from for loop in naming new variables
David Winsemius
dwinsemius at comcast.net
Thu Sep 16 18:14:08 CEST 2010
On Sep 16, 2010, at 11:44 AM, Maas James Dr (MED) wrote:
> Simple one here ... but can't get it to work ...
>
> for (i in 1:4){
> paste("stuff",[i]),sep="") <- 3 + i
> }
>
> ls()
> rm(list=ls())
> for (i in 1:4){
+ paste("stuff",[i]),sep="") <- 3 + i
Error: unexpected '[' in:
"for (i in 1:4){
paste("stuff",["
So the first error pointed you to a problem
> }
Error: unexpected '}' in "}"
Fixing that error points you to another:
> for (i in 1:4){
+ paste("stuff", i),sep="") <- 3 + i
Error: unexpected ',' in:
"for (i in 1:4){
paste("stuff", i),"
And when the syntax gets unsnarled you find that you are trying not
using the correct semantics for assignment:
> for (i in 1:4){
+ paste("stuff",i,sep="") <- 3 + i
+ }
Error in paste("stuff", i, sep = "") <- 3 + i :
target of assignment expands to non-language object
You are currently attempting to "assign" a numeric value to a
character vector. (That is, once stop throwing an error because
putting the square brackets around "i" ... and you lose the right-
paren before the sep argument.) Try looking at the help page for
assign (and working more basic examples):
?assign.
>
>
>
> I just want it to create 4 new variables called stuff1, stuff2,
> stuff3, stuff4 with the corresponding assignments. I realise that
> there are more elegant functions but this is just a model of a
> bigger situation.
>
> Thanks
>
> Jim
>
>
> ===============================
> Dr. Jim Maas
> University of East Anglia
>
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> 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.
David Winsemius, MD
West Hartford, CT
More information about the R-help
mailing list