Repeat Multiple NULLs in R

In my simulation, I need a vector that looks like this:

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

However, in R, when I use rep(NULL, 4) , it does not return anything. For instance,

 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!

+7
source share
2 answers

NULL has no length:

 > length(NULL) [1] 0 

Therefore, you cannot insert it into a vector. You can either have NA in your vectors, or have a list with NULL elements.

 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)) 
+7
source

I ran into this problem today and the decision I made does not work for me. Here's how to create a list of length n with all NULL elements -

 l <- vector(mode = 'list', length = 5); l [[1]] NULL [[2]] NULL [[3]] NULL [[4]] NULL [[5]] NULL 
0
source

Source: https://habr.com/ru/post/956796/


All Articles