Home > other >  In R how to extract the position or NULL entries in a lis
In R how to extract the position or NULL entries in a lis

Time:09-10

Using R how can I print the position of NULL entries in a list.

My list looks like this

list 
   name1    character[1]    xxxx
   name2    character[1]    xxxx
   name3    NULL            Pairlist of length 0

How can I simply just extract 3 as the positions of the list which are NULL.

I used list2 <- list[sapply(list, is.null)] to extract the whole entry of the NULL entries in the list, but I simply just want to print 3 for further work.

CodePudding user response:

Use which:

l <- list(NULL, mtcars, NULL)
which(sapply(l, is.null))
#[1] 1 3

CodePudding user response:

lengths could work as well

 which(!lengths(list))
[1] 1 3
  • Related