Dimensioning lists.

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Dimensioning lists.

Rolf Turner

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

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: [External] Dimensioning lists.

Richard M. Heiberger
Use double [[ ]]

> xxx[[2,3]]
$a
[1] "n"

$b
[1] 20

>

________________________________________
From: R-help <[hidden email]> on behalf of Rolf Turner <[hidden email]>
Sent: Sunday, February 14, 2021 10:35 PM
To: "[hidden email]\"  <[hidden email]>"@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

______________________________________________
[hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Dimensioning lists.

R help mailing list-2
In reply to this post by Rolf Turner
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 <[hidden email]> On Behalf Of Rolf Turner
Sent: Sunday, February 14, 2021 10:35 PM
To: "[hidden email]" <[hidden email]>"@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] 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

______________________________________________
[hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: [External] Dimensioning lists.

Rolf Turner
In reply to this post by Rolf Turner

Thanks to Richard Heiberger, John Fox, and Avi Gross, who pointed out
to me that I should

> use double brackets
> [[ ]]
>
> > xxx[[2,3]]
> $a
> [1] "n"
>
> $b
> [1] 20

I really should've should've known that; I feel a bit duh now!

See fortunes::fortune(341) for a relevant comment. :-)

Thanks again.

cheers,

Rolf Turner

--
Honorary Research Fellow
Department of Statistics
University of Auckland
Phone: +64-9-373-7599 ext. 88276

______________________________________________
[hidden email] 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.