lengths for recursive lists
Arguments
- x
list
or vector- doSum
logical
indicating whether to return the overall sum of lengths. WhenNULL
it will return the aggregate length of each list element inx
. WhenFALSE
it will return the same list structure of x, with the length of each. WhenTRUE
it will return the total length of all elements inx
as one value.- ...
additional parameters are ignored
Value
integer
value, vector, or list:
When
doSum is NULL
(default) it returns aninteger
vector with lengthlength(x)
and namesnames(x)
, whose values are the total number of elements in each item inx
after runningbase::unlist()
.When
doSum=="TRUE"
, it returns the singleinteger
length of all elements inx
.When
doSum=="FALSE"
, it returns the full structure ofx
with theinteger
length of each element.
The parameter doSum
is intended for internal use, during
recursive calls of rlengths()
to itself. When doSum is NULL
or
TRUE
, recursive calls to rlengths()
set doSum=TRUE
.
Details
This function takes a list as input, and returns the length
of each list element after running base::unlist()
.
See also
Other jam list functions:
cPaste()
,
heads()
,
jam_rapply()
,
list2df()
,
mergeAllXY()
,
mixedSorts()
,
rbindList()
,
relist_named()
,
sclass()
,
sdim()
,
uniques()
,
unnestList()
Examples
x <- list(
A=list(
A1=nameVector(1:3, letters[1:3]),
A2=list(
A1a=nameVector(4:7, letters[4:7]),
A1b=nameVector(11:14, letters[11:14]))),
B=list(B1=nameVector(1:9, letters[1:9]),
B2=nameVector(20:25, letters[20:25])));
# default lengths(x) shows length=2 for A and B
lengths(x)
#> A B
#> 2 2
# rlengths(x) shows the total length of A and B
rlengths(x)
#> A B
#> 11 15