[R] [SOLVED] Accessing named members of a list in an array

Jeff Newmiller jdnewmil at dcn.davis.CA.us
Sun Jul 1 18:29:24 CEST 2012


And when you get tired of waiting for your elegant solution to finish, you can go back and convert to vectors or 3d arrays.

Lists are good for building structures, but not particularly good for computation.
---------------------------------------------------------------------------
Jeff Newmiller                        The     .....       .....  Go Live...
DCN:<jdnewmil at dcn.davis.ca.us>        Basics: ##.#.       ##.#.  Live Go...
                                      Live:   OO#.. Dead: OO#..  Playing
Research Engineer (Solar/Batteries            O.O#.       #.O#.  with
/Software/Embedded Controllers)               .OO#.       .OO#.  rocks...1k
--------------------------------------------------------------------------- 
Sent from my phone. Please excuse my brevity.

mlell08 <mlell08 at gmail.com> wrote:

>> 
>
>> You thought wrong (on two accounts as it happens). The "$" methods
>> translate to "[[" with a quoted argument and there is no
>> matrix/array  equivalent since vectors loose their names (if they
>> had any to begin with) when they are put into a matrix or array.
>> The equivalent method to x$a is x[["a"]], not x["a"].
>
>
>On 01.07.2012 08:45, Patrick Burns wrote:
>> Your problem is that you are trying to use `$` on an atomic vector
>> rather than a list:
>> 
>>> a<- array(list(NULL),dim=c(2,2)) a[[1,1]] <- c(a=2,b=3) 
>>> a[[1,1]]$a
>> Error in a[[1, 1]]$a : $ operator is invalid for atomic vectors
>>> a[[1,1]]
>> a b 2 3
>>> a[[1,1]] <- list(a=2,b=3) a[[1,1]]$a
>> [1] 2
>>> a[[1,1]]
>> $a [1] 2
>> 
>> $b [1] 3
>> 
>> 
>> From the description of the problem, perhaps it would be easier to
>> just have a 3-dimensional array.
>> 
>> Pat
>> 
>> 
>> On 30/06/2012 14:35, mlell08 wrote:
>>> Dear List,
>>> 
>>> I've created a two-dimensional array which shall contain a value
>>> and its error, respectively. These two values are concatenated in
>>> al list and bear the names "sl" and "sl_err"
>>> 
>>> But I can't adress them using the $-notation.
>>> 
>>> a<- array(list(NULL),dim=c(2,2)) a[[1,1]]<- c(a=2,b=3) 
>>> a[[1,1]]$a ## Fehler in a[[1, 1]]$a : $ operator is invalid for
>>> atomic vectors a[[1,1]]["a"]       # This works however. ## a ##
>>> 2
>>> 
>>> I always thought these two methods of indexing are equal? Is
>>> there any way to use the $-Style indexing?
>>> 
>>> Thank you, Moritz
>>> 
>> 
>
>Hello,
>
>thank you for all your answers!
>I found out that I can circumvent the Problem "atomic vector" by
>making the object which I extracted a list by
>
>class(a[[1,1]]) <- "list"
>
>After all, it seems a bit unlogical to me, because
>1) c(a=2, b=3)$a works as expected
>
>2) I've created a matrix containing lists and not atomic vectors by
>   a<- array(list(NULL),dim=c(2,2))
>   however, my added element seems to be converted to an atomic vector
>   - without need because
>
>3) I avoided vectorization (thanks for the link to R inferno! Very apt
>   comparison;-)) by using the [[ operator for assignment
>   (which seemed to work because else more than one matrix element
>  would have been filled) so there should be no need for dropping names
>   (which actually doesn't happen, because a[[1,1]]["a"] works
>
>but if the operator [[<- works like this, there is no point in
>argueing?
>
>So - what works:
>
>a<- array(list(NULL),dim=c(2,2))
>a[[1,1]]<- c(a=2,b=3)
>a[[1,1]]
>## a b
>## 2 3
>
>a[[1,1]]$a
>## Fehler in a[[1, 1]]$a : $ operator is invalid for atomic vectors
>
>#Synonym??
>a[[1,1]][["a",exact=FALSE]]
>## 2
>
>a[[1,1]]["a"]
>## a
>## 2
>
>class(a[[1,1]])<- "list"
>a[[1,1]]$a
>## 2
>
>Regards, Moritz
>
>-- 
>GnuPG Key: 0x7340821E
>
>______________________________________________
>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