I am doing a series of things in dplyr , tidyr , so if possible I would like to support the solution across the channels.
I have a list with unequal numbers of elements in each component:
lolz <- list(a = c(2,4,5,2,3), b = c(3,3,2), c=c(1,1,2,4,5,3,3), d=c(1,2,3,1), e=c(5,4,2,2)) lolz $a [1] 2 4 5 2 3 $b [1] 3 3 2 $c [1] 1 1 2 4 5 3 3 $d [1] 1 2 3 1 $e [1] 5 4 2 2
I am wondering if there is a neat one liner to fill each element with NA so that they all have the same length as the element with maximum elements:
I have 2 liner:
lolz %>% lapply(length) %>% unlist %>% max -> mymax lolz %>% lapply(function(x) c(x, rep(NA, mymax-length(x)))) $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
I wonder if I miss something faster / more elegant.
source share