[R] sibling list element reference during list definition

Adaikalavan Ramasamy ramasamy at cancer.org.uk
Sat Nov 12 14:14:08 CET 2005


It would be more interesting to ask why does this does not work.

   mylist <- list( value=5, plusplus = mylist$value + 1 )

I think this is because plusplus cannot be evaluated because mylist does
not exist and mylist cannot be created until plusplus is evaluated.

There are people on this list who can explain in more technical terms.
But I think reading this page might help
http://cran.r-project.org/doc/manuals/R-lang.html#index-evaluation_002c-symbol-166


Here is one option :

 mylist <- eval( expression( list( value=x, plusplus=x+1) ), list(x=5) )
 mylist
 $value
 [1] 5
 $plusplus
 [1] 6


Or a bit easier to read is :

 myfun  <- function(x) list( value=x, plusplus=x+1 )
 mylist <- myfun(5)


Regards, Adai


On Sat, 2005-11-12 at 01:03 -0600, Paul Roebuck wrote:
> Can the value of a list element be referenced from a
> sibling list element during list creation without the use
> of a temporary variable?
> 
> The following doesn't work but it's the general idea.
> 
> > list(value = 2, plusplus = $value+1)
> 
> such that the following would be the output from str()
> 
> List of 2
>  $ value   : num 2
>  $ plusplus: num 3
> 
> ----------------------------------------------------------
> SIGSIG -- signature too long (core dumped)
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>




More information about the R-help mailing list