I have vectors with varying length, like 3,7,9,19etc. I’d like to use these vectors to fill in multiple columns in each row. I have 19 columns in total.
When the vector length is smaller than 19, I’d like to fill those columns with NA. How should I do it in R? Thanks!
You can use `length<-`
, for example
# dummy data `x`
> (x <- lapply(c(3, 7, 9, 19), seq_len))
[[1]]
[1] 1 2 3
[[2]]
[1] 1 2 3 4 5 6 7
[[3]]
[1] 1 2 3 4 5 6 7 8 9
[[4]]
[1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
> Map(`length<-`, x, 19)
[[1]]
[1] 1 2 3 NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
[[2]]
[1] 1 2 3 4 5 6 7 NA NA NA NA NA NA NA NA NA NA NA NA
[[3]]
[1] 1 2 3 4 5 6 7 8 9 NA NA NA NA NA NA NA NA NA NA
[[4]]
[1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19