[R] Suggestion on how to make permanent changes to a singleobject in a list?

Bert Gunter gunter.berton at gene.com
Thu Jan 3 23:08:54 CET 2008


Gentlemen:

I'm sorry, I don't see the problem. R's is Lisp (or Scheme)-inspired, so you
need to think in terms of lists, or equivalently, trees. So what you seem to
want to do is easily navigate down a tree to modify a node. This is fairly
easy to do with list indexing:

## First create a tree with 5 top nodes each of which has 3 child leaf nodes

## each of which is an empty list.

fooStack<-lapply(1:5,function(x)lapply(1:3,function(x)list()))

## Now modify the <4,2> leaf list:

fooStack[[c(4,2)]]$bar <- "bar"
## Note: This is shorthand for fooStack[[4]][[2]]$bar <- "bar"

> fooStack
[[1]]
[[1]][[1]]
list()

[[1]][[2]]
list()

[[1]][[3]]
list()


[[2]]
[[2]][[1]]
list()

[[2]][[2]]
list()

[[2]][[3]]
list()


[[3]]
[[3]][[1]]
list()

[[3]][[2]]
list()

[[3]][[3]]
list()


[[4]]
[[4]][[1]]
list()

[[4]][[2]]
[[4]][[2]]$bar
[1] "bar"


[[4]][[3]]
list()


[[5]]
[[5]][[1]]
list()

[[5]][[2]]
list()

[[5]][[3]]
list()

Cheers,

Bert Gunter
Genentech


-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On
Behalf Of Charilaos Skiadas
Sent: Thursday, January 03, 2008 1:46 PM
To: Peter Waltman
Cc: r-help at r-project.org
Subject: Re: [R] Suggestion on how to make permanent changes to a
singleobject in a list?

You might want to consider using the proto package. Otherwise,  
functions that end in <- have the ability to alter their arguments.  
Look at the following (admittedly not very natural) construct:

`fooModifier<-` <- function( foo, value ) {
     foo$bar <- "bar"
}

fooModifier( fooStack[[ 1 ]] ) <- NULL

 > fooStack
[[1]]
[1] "bar"

[[2]]
list()

[[3]]
list()

[[4]]
list()

[[5]]
list()


I agree, that it would be nice to be able to do this more easily.
Haris Skiadas
Department of Mathematics and Computer Science
Hanover College


On Jan 3, 2008, at 4:35 PM, Peter Waltman wrote:

>
>    specifically, imagine we have:
>
>      fooStack <- list()
>      for ( i in 1:5 )
>          fooStack[[i]] <- list()
>
>    and we have a function:
>
>      fooModifier <- function( foo ) {
>
>      foo$bar <- "bar"
>
>      }
>
>    then, if we invoke fooModifier, i.e.:
>
>      fooModifier( fooStack[[ 1 ]] )
>
>    the $bar elt is only set in the scope of the function, and if we  
> use the
>    "<<-" modifier in fooModifier, R will throw an error b/c it  
> can't find the
>    "foo" object.  I have to say that for someone coming from  
> languages that
>    have pointers and/or references, it's really frustrating that R  
> fails to
>    allow one to have direct access to the objects' memory space.
>    Onyway, one workaround would be to pass in the whole fooStack  
> object and the
>    index of the elt that you want to modify to the fooModifier fn,  
> but I'd
>    rather not have to pass the whole thing in.
>    Any suggestions?
>    Thanks!
>    Peter Waltman

______________________________________________
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