Apply head() across each element in a list of vectors
Arguments
- x
 listof atomic vectors, assumed to be the same atomic type.- n
 integermaximum number of items to include from each element in the listx. Whenncontains multiple values, they are recycled tolength(x)and applied to each list element in order.- ...
 additional arguments are passed to
utils::head().
Details
Note that this function currently only operates on a list
of vectors. This function is notably faster than
lapply(x, head, n) because it operates on the entire
vector in one step.
Also the input n can be a vector so that each element in
the list has a specific number of items returned.
See also
Other jam list functions:
cPaste(),
jam_rapply(),
list2df(),
mergeAllXY(),
mixedSorts(),
rbindList(),
relist_named(),
rlengths(),
sclass(),
sdim(),
uniques(),
unnestList()
Examples
l <- list(a=1:10, b=2:5, c=NULL, d=1:100);
heads(l, 1);
#> $a
#> [1] 1
#> 
#> $b
#> [1] 2
#> 
#> $c
#> integer(0)
#> 
#> $d
#> [1] 1
#> 
heads(l, 2);
#> $a
#> [1] 1 2
#> 
#> $b
#> [1] 2 3
#> 
#> $c
#> integer(0)
#> 
#> $d
#> [1] 1 2
#> 
heads(l, n=c(2, 1, 3, 5))
#> $a
#> [1] 1 2
#> 
#> $b
#> [1] 2
#> 
#> $c
#> integer(0)
#> 
#> $d
#> [1] 1 2 3 4 5
#>