[R] [External] Dimensioning lists.
Richard M. Heiberger
rmh @end|ng |rom temp|e@edu
Mon Feb 15 05:13:14 CET 2021
Use double [[ ]]
> xxx[[2,3]]
$a
[1] "n"
$b
[1] 20
>
________________________________________
From: R-help <r-help-bounces using r-project.org> on behalf of Rolf Turner <r.turner using auckland.ac.nz>
Sent: Sunday, February 14, 2021 10:35 PM
To: "r-help using R-project.org\" <r-help using 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] 20
That 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 using 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.
More information about the R-help
mailing list