Paul Roebuck
2005-Nov-12 07:03 UTC
[R] sibling list element reference during list definition
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)
Adaikalavan Ramasamy
2005-Nov-12 13:14 UTC
[R] sibling list element reference during list definition
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 >
Gabor Grothendieck
2005-Nov-12 14:24 UTC
[R] sibling list element reference during list definition
You can do this: L <- list(value = 2) L$plusplus <- L$value + 1 or use the proto package which does support this sort of manipulation: library(proto) as.list(proto(,{value = 2; plusplus = value+1})) That creates a proto object with the indicated two components and then converts it to a list. On 11/12/05, Paul Roebuck <roebuck at mdanderson.org> 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 >