I have a list (ll) of lists (l1,l2,l3) that have the same length.
l1 <- list( c("a","b","c") , c("d","e","f") , c(NULL) )
l2 <- list( c("a","b") , c("d","e") , c(NULL) )
l3 <- list( c("a") , c("d") , c("g") )
ll <- list(l1,l2,l3)
str(ll)
List of 3
$ :List of 3
..$ : chr [1:3] "a" "b" "c"
..$ : chr [1:3] "d" "e" "f"
..$ : NULL
$ :List of 3
..$ : chr [1:2] "a" "b"
..$ : chr [1:2] "d" "e"
..$ : NULL
$ :List of 3
..$ : chr "a"
..$ : chr "d"
..$ : chr "g"
is there a way to create a (summary?) table that counts/compares how many items are in each list, whitout using for/loop?
i'm thinking of an output like this:
list 1 2 3
l1 3 3 0
l2 2 2 0
l3 1 1 1
We can use lengths
with lapply
do.call(rbind, lapply(ll, lengths))
# [,1] [,2] [,3]
#[1,] 3 3 0
#[2,] 2 2 0
#[3,] 1 1 1
Or with lengths
on simplify2array
lengths(simplify2array(ll))
# [,1] [,2] [,3]
#[1,] 3 2 1
#[2,] 3 2 1
#[3,] 0 0 1
Taking the transpose gets the expected output
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With