Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

repeat multiple NULL in R

Tags:

r

In my simulation I need a vector that looks like:

vec = NULL NULL NULL NULL 2 2 2 2 4 4 4 4

However, in R when I use rep(NULL, 4) it returns nothing. For example,

vec.all = c(rep(NULL, 4), rep(2, 4), rep(4, 4))
vec.all
2 2 2 2 4 4 4 4

Is there a way to repeat NULL several times in R? Thanks!

like image 266
alittleboy Avatar asked Oct 26 '13 17:10

alittleboy


1 Answers

NULL has no length:

> length(NULL)
[1] 0

So you can't really insert it into a vector. You can either have NA in you vectors or have a list with NULL items.

vec.all = c(rep(NA, 4), rep(2, 4), rep(4, 4))

list.all = c(rep(list(NULL), 4), rep(list(2), 4), rep(list(4), 4))
like image 59
flodel Avatar answered Nov 03 '22 02:11

flodel