R: Adding an empty vector to the list

In R, when I add an empty list to the list, it is not actually added, as shown below:

a = list() a[[1]] = c() print(length(a)) 

length(a) 0 will be printed in this case, and if I change the 2nd line to a[[1]] = c(1) , then the length of the list will be 1, as expected. In my implementation, I need the length of the list that needs to be changed, even if I add an empty list to the list. Is there any way to do this?

+5
source share
1 answer

Using things like vector() (boolean) and character() assign a specific class to the element that you want to remain "empty". I would use the NULL class. An NULL list item can be assigned using [<- instead of [[<- .

 a <- list() a[1] <- list(NULL) a # [[1]] # NULL length(a) # [1] 1 
+3
source

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


All Articles