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

arun smartpink111 at yahoo.com
Sat Jun 30 18:39:56 CEST 2012


Hi,

You can use these as well to access named members.

> a[1]
[[1]]
a b 
2 3 


 >a[1][[1]][1]
a 
2 

> a[[1,1]][1]
a 
2 
 >a[[1,1]][2]
b 
3 

 >identical(a[[1,1]]["a"],a[[1,1]][1],a[1][[1]][1])
[1] TRUE


> a[[1,1]][["a"]]
[1] 2
> a[[1,1]][["b"]]
[1] 3

or,

> a[[c(1,2)]]
[1] 3
 >a[[c(1,1)]]
[1] 2


>a$ab<-a[[1]]
> a$ab
a b 
2 3 
 >a$ab[1]
a 
2 
>a$ab1<-a[[1]][1]
>a$ab1
a 
2 


Hope this will be of some use for you


A.K.



----- Original Message -----
From: mlell08 <mlell08 at gmail.com>
To: r-help at r-project.org
Cc: 
Sent: Saturday, June 30, 2012 9:35 AM
Subject: [R]  Accessing named members of a list in an array

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

-- 
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