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
On Jun 30, 2012, at 9:35 AM, 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?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"].> 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.David Winsemius, MD West Hartford, CT
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$aba b 2 3 ?>a$ab[1] a 2>a$ab1<-a[[1]][1] >a$ab1a 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.
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 >-- Patrick Burns pburns at pburns.seanet.com twitter: @portfolioprobe http://www.portfolioprobe.com/blog http://www.burns-stat.com (home of 'Some hints for the R beginner' and 'The R Inferno')