[R] list creation interpolation
Greg Snow
Greg.Snow at imail.org
Fri Nov 21 20:41:54 CET 2008
The $ syntax for working with elements of a list is a magical shortcut for [[]]. It is a great shortcut when used as intended, but trying to force magical shortcuts to do things that they were not intended for usually results in the programming equivalent of turning yourself into a toad.
The correct approach is to not use the shortcut, but the thing that it is a shortcut for.
Did you try:
> resultlist[[ a[1] ] <- foo
> resultlist[[ a[2] ] <- rnorm(20)
Hope this helps,
--
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 H. Paul Benton
> Sent: Friday, November 21, 2008 10:58 AM
> To: r-help at r-project.org
> Subject: [R] list creation interpolation
>
> Hello all,
>
>
>
> I apologize if this is simple or has already been answered somewhere,
> but
> I'm not sure what to search for although I have tried and didn't come
> up
> with anything so.. Here's my question.
>
> How can I interpolate list names or do I have to do it post list
> creation.
> Since that's not very clear here is some sample code of what I wanted
> to do:
>
>
>
> > resultlist<-list()
>
> > a<-c("Book", "video", "radio", "mp3")
>
> > foo<-rnorm(10)
>
> > resultlist$a[1]<-foo
>
> Warning message:
>
> In resultlist$a[1] <- foo :
>
> number of items to replace is not a multiple of replacement length
>
> >
>
>
>
> What I wanted here was a named list so that I would have 4 values the
> list
> would be
>
> resultlist$Book
>
> resultlist$video
>
> resultlist$radio
>
> resultlist$mp3
>
>
>
> Any idea on how to do this other than doing it post creation ie
>
>
>
> resultlist[[1]]<-foo
>
> resultlist[[2]]<-rnorm(20) etc..
>
> names(resultlist)<-a
>
>
>
>
>
>
>
> Cheers,
>
>
>
> Paul
>
>
>
>
> [[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.
More information about the R-help
mailing list