Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to expand a list with NULLs up to some length?

Given a list whose length <= N, what is the best / most efficient way to fill it up with trailing NULLs up to length (so that it has length N).

This is something which is a one-liner in any decent language, but I don't have a clue how to do it (efficiently) in a few lines in R so that it works for every corner case (zero length list etc.).

like image 234
eold Avatar asked Nov 11 '11 17:11

eold


2 Answers

Let's keep it really simple:

tst<-1:10 #whatever, to get a vector of length 10
tst<-tst[1:15]
like image 131
Nick Sabbe Avatar answered Oct 01 '22 01:10

Nick Sabbe


Try this :

> l = list("a",1:3)
> N = 5
> l[N+1]=NULL
> l
[[1]]
[1] "a"

[[2]]
[1] 1 2 3

[[3]]
NULL

[[4]]
NULL

[[5]]
NULL

>
like image 32
Matt Dowle Avatar answered Oct 01 '22 01:10

Matt Dowle