hkj447
hkj447

Reputation: 737

using sapply with two or more variables

I would like to evaluate two different variables using is.null:

a<-c(1,2)
b<-NULL
sapply(c(a,b),is.null)

However, I got a warning that stating that the condition has length > 1 and only the first element will be used. The output I got showed this as it only evaluted FALSE, FALSE rather than FALSE, FALSE, TRUE.

How can I successfully run the is.null function on multiple variables?

Upvotes: 3

Views: 146

Answers (1)

akrun
akrun

Reputation: 886948

Here, the issue is that NULL cannot exist in a vector. When we do c, we are concatenating one vector with another and whatever NULL elements are present in one vector gets lost. Instead, place it in a list

sapply(c(a, list(b)), is.null)

Upvotes: 3

Related Questions