kli
kli

Reputation: 283

R List: How to get the length from a list stored in a key-value list?

If I stored a list or vector in a key-value list, the list's length is 1 returned by length().

> i<-c(0,1,2,3,4)
> length(i)
[1] 5
> l <- list( r=2, i=i)
> l["i"]
$i
[1] 0 1 2 3 4

> length(l["i"])
[1] 1

Why? How to store a list or vector in a key-value list properly?

Upvotes: 1

Views: 2879

Answers (1)

hd1
hd1

Reputation: 34657

length(l$i) will do what you want...

> i<-c(0,1,2,3,4)
> i<-c(0,1,2,3,4)
> l <- list( r=2, i=i)
> l[i]
$r
[1] 2

$i
[1] 0 1 2 3 4

$<NA>
NULL

$<NA>
NULL

> l$i
[1] 0 1 2 3 4
> length(l$i)
[1] 5

Upvotes: 1

Related Questions