I have a setting in which it would be convenient to treat a list as an array, i.e. to address its entries via a pair of indices. A toy example: xxx <- vector("list",9) set.seed(42) for(i in 1:9) xxx[[i]] <- list(a=sample(letters,1),b=sample(1:100,1)) I would like to be able to treat "xxx" as a 3 x 3 matrix. I tried dim(xxx) <- c(3,3) When I do, e.g. xxx[2,3] I get:> [[1]] > [[1]]$a > [1] "n" > > [[1]]$b > [1] 20That is I get a list of length 1, whose (sole) entry is the desired object. I would *like* to get just the desired object, *not* wrapped in a list, i.e.:> $a > [1] "n" > > $b > [1] 20 >(which is what I get by typing xxx[2,3][[1]]). Is there any way to prevent the entries of xxx from being wrapped up in lists of length 1? Thanks for any enlightenment. cheers, Rolf Turner -- Honorary Research Fellow Department of Statistics University of Auckland Phone: +64-9-373-7599 ext. 88276
Use double [[ ]]> xxx[[2,3]]$a [1] "n" $b [1] 20>________________________________________ From: R-help <r-help-bounces at r-project.org> on behalf of Rolf Turner <r.turner at auckland.ac.nz> Sent: Sunday, February 14, 2021 10:35 PM To: "r-help at R-project.org\" <r-help at R-project.org>"@r-project.org Subject: [External] [R] Dimensioning lists. I have a setting in which it would be convenient to treat a list as an array, i.e. to address its entries via a pair of indices. A toy example: xxx <- vector("list",9) set.seed(42) for(i in 1:9) xxx[[i]] <- list(a=sample(letters,1),b=sample(1:100,1)) I would like to be able to treat "xxx" as a 3 x 3 matrix. I tried dim(xxx) <- c(3,3) When I do, e.g. xxx[2,3] I get:> [[1]] > [[1]]$a > [1] "n" > > [[1]]$b > [1] 20That is I get a list of length 1, whose (sole) entry is the desired object. I would *like* to get just the desired object, *not* wrapped in a list, i.e.:> $a > [1] "n" > > $b > [1] 20 >(which is what I get by typing xxx[2,3][[1]]). Is there any way to prevent the entries of xxx from being wrapped up in lists of length 1? Thanks for any enlightenment. cheers, Rolf Turner -- Honorary Research Fellow Department of Statistics University of Auckland Phone: +64-9-373-7599 ext. 88276 ______________________________________________ R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see 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.
Rolf, Try: xxx[[2,3]] The double bracket return an item, not a list containing the item.> xxx[2,3][[1]] [[1]]$a [1] "m" [[1]]$b [1] 95> xxx[[2,3]]$a [1] "m" $b [1] 95 -----Original Message----- From: R-help <r-help-bounces at r-project.org> On Behalf Of Rolf Turner Sent: Sunday, February 14, 2021 10:35 PM To: "r-help at R-project.org" <r-help at R-project.org>"@r-project.org Subject: [R] Dimensioning lists. I have a setting in which it would be convenient to treat a list as an array, i.e. to address its entries via a pair of indices. A toy example: xxx <- vector("list",9) set.seed(42) for(i in 1:9) xxx[[i]] <- list(a=sample(letters,1),b=sample(1:100,1)) I would like to be able to treat "xxx" as a 3 x 3 matrix. I tried dim(xxx) <- c(3,3) When I do, e.g. xxx[2,3] I get:> [[1]] > [[1]]$a > [1] "n" > > [[1]]$b > [1] 20That is I get a list of length 1, whose (sole) entry is the desired object. I would *like* to get just the desired object, *not* wrapped in a list, i.e.:> $a > [1] "n" > > $b > [1] 20 >(which is what I get by typing xxx[2,3][[1]]). Is there any way to prevent the entries of xxx from being wrapped up in lists of length 1? Thanks for any enlightenment. cheers, Rolf Turner -- Honorary Research Fellow Department of Statistics University of Auckland Phone: +64-9-373-7599 ext. 88276 ______________________________________________ R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see 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.