Nneka
Nneka

Reputation: 1850

is.null returns false despite fo being TRUE in R

i have a variable :

x$value
[[1]]
NULL

but when i run is.null(x$value) i get:

is.null(x$value)
[1] FALSE

how is this possible? How can i fix this? i am using this inside an sum(sapply((data$iteration), FUN = function(x) {ifelse(is.null(x$value), 0, x$value)}), na.rm = TRUE) an getting an error:

Error in sum(sapply((data$iteration), FUN = function(x) { : 
  invalid 'type' (list) of argument

Upvotes: 0

Views: 525

Answers (1)

Jonas
Jonas

Reputation: 1810

The problem is that x$value is a list containing one element which is NULL, hence x$value is not NULL. You need to use x$value[[1]] to access this list, then you get that is.null(x$value[[1]]) is TRUE.

Upvotes: 6

Related Questions