Skip to content
Prev 298805 / 398503 Next

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


        
On 01.07.2012 08:45, Patrick Burns wrote:
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