尽管fo在R中为TRUE,但is.null仍返回false
我有一个变量:
x$value
[[1]]
NULL
但是当我跑步时,is.null(x$value)我得到:
is.null(x$value)
[1] FALSE
这怎么可能?我怎样才能解决这个问题?我在sum(sapply((data$iteration), FUN = function(x) {ifelse(is.null(x$value), 0, x$value)}), na.rm = TRUE)一个错误中使用它:
Error in sum(sapply((data$iteration), FUN = function(x) { :
invalid 'type' (list) of argument
回答
问题是这x$value是一个包含一个元素的列表,该元素是NULL,因此x$value不是NULL。您需要使用x$value[[1]]来访问此列表,那么你得到的is.null(x$value[[1]])是TRUE。