I created variables automatically like this way
for(i in 1:5){
nam <- paste("a",i,sep="")
assign(nam,1:i)
}
and then, i want to insert a new data into "a2" variable. so, i did
next
sentence
paste("a",2,sep="") <- 4
so, i got this error message
Error in get(paste("a", 2, sep = ""))[1] <- 4 :
target of assignment expands to non-language object
anyone knows abou this error message and tell me how to solve thie problem,
please......
--
View this message in context:
http://n4.nabble.com/somebody-help-me-about-this-error-message-tp1571700p1571700.html
Sent from the R help mailing list archive at Nabble.com.
You forgot the assign the second time:
assign(paste("a",2,sep=""), 4)
does what you want.
Hope this helps a little
Allan.
On 27/02/10 05:13, Joseph Lee wrote:> I created variables automatically like this way
>
> for(i in 1:5){
> nam<- paste("a",i,sep="")
> assign(nam,1:i)
> }
>
> and then, i want to insert a new data into "a2" variable. so, i
did next
> sentence
>
> paste("a",2,sep="")<- 4
>
> so, i got this error message
>
> Error in get(paste("a", 2, sep = ""))[1]<- 4 :
> target of assignment expands to non-language object
>
> anyone knows abou this error message and tell me how to solve thie problem,
> please......
>
paste("a",2,sep="") is simply creating a new character
"a2"
Why not just
a2 <- 4
?
--- On Sat, 2/27/10, Joseph Lee <seokhyung83 at gmail.com> wrote:
> From: Joseph Lee <seokhyung83 at gmail.com>
> Subject: [R] somebody help me about this error message...
> To: r-help at r-project.org
> Received: Saturday, February 27, 2010, 12:13 AM
>
> I created variables automatically like this way
>
> for(i in 1:5){
> ??? nam <- paste("a",i,sep="")
> ??? assign(nam,1:i)
> }
>
> and then, i want to insert a new data into "a2" variable.
> so, i did next
> sentence
>
> paste("a",2,sep="") <- 4
>
> so, i got this error message
>
> Error in get(paste("a", 2, sep = ""))[1] <- 4 :
> ? target of assignment expands to non-language object
>
> anyone knows abou this error message and tell me how to
> solve thie problem,
> please......
> --
> View this message in context:
http://n4.nabble.com/somebody-help-me-about-this-error-message-tp1571700p1571700.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> 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.
>
__________________________________________________________________
Looking for the perfect gift? Give the gift of Flickr!
http://www.flickr.com/gift/
Others answered your question. I would however suggest that you asked the wrong
question.
If you really feel the need for your future self to serve some serious penance
for past or near future misdeeds, then continuing with this strategy is one way
to do that (just please don't inflict your suffering on us when this ends up
causing more problems than it solves).
If on the other hand, you want to avoid future headaches and become a better
programmer, then learn to use lists rather than global variables.
a <- list()
for( i in 1:5 ) {
a[[ paste('a',i,sep="") ]] <- 1:i
### or simpler a[[i]] <- 1:i
}
If you use the simpler version, then you could still get the names by:
names(a) <- paste('a', 1:5, sep="")
Then
a[[2]] <- 4
a[['a2']] <- 4
a$a2 <- 4
a <- within(a, a2<-4)
will all do the substitutions.
See the help for the within and with functions as well as fortune(236) and the
last few lines of FAQ 7.21.
--
Gregory (Greg) L. Snow Ph.D.
Statistical Data Center
Intermountain Healthcare
greg.snow at imail.org
801.408.8111
> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
> project.org] On Behalf Of Joseph Lee
> Sent: Friday, February 26, 2010 10:14 PM
> To: r-help at r-project.org
> Subject: [R] somebody help me about this error message...
>
>
> I created variables automatically like this way
>
> for(i in 1:5){
> nam <- paste("a",i,sep="")
> assign(nam,1:i)
> }
>
> and then, i want to insert a new data into "a2" variable. so, i
did
> next
> sentence
>
> paste("a",2,sep="") <- 4
>
> so, i got this error message
>
> Error in get(paste("a", 2, sep = ""))[1] <- 4 :
> target of assignment expands to non-language object
>
> anyone knows abou this error message and tell me how to solve thie
> problem,
> please......
> --
> View this message in context: http://n4.nabble.com/somebody-help-me-
> about-this-error-message-tp1571700p1571700.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> 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.