Add NAs to make all list elements equal length

You could use

lapply(lolz, `length<-`, max(lengths(lolz)))
# $a
# [1]  2  4  5  2  3 NA NA
# 
# $b
# [1]  3  3  2 NA NA NA NA
# 
# $c
# [1] 1 1 2 4 5 3 3
# 
# $d
# [1]  1  2  3  1 NA NA NA
# 
# $e
# [1]  5  4  2  2 NA NA NA

or

n <- max(lengths(lolz))
lapply(lolz, `length<-`, n)

Leave a Comment

tech