1

How can I turn the 3 item output of the for loop below into a data frame. In attempting a solution, I've tried:

-Creating an object related to the for loop, but couldn't succeed -Creating a matrix, to no effect

What code would turn the output into a vector or list?

> for(i in X$Planned)ifelse(is.na(i),print("ISNA"),print("NOTNA"))
[1] "NOTNA"
[1] "NOTNA"
[1] "ISNA"
Hector Haffenden
  • 1,290
  • 9
  • 23
Ramon
  • 13
  • 5

1 Answers1

1
sapply(x$Planned, function(elem) if (is.na(elem)) {"isNA"} else {"notNA"})

# this will do it!

# however, it will be slower than the vectorized form

ifelse(is.na(x$Planned), "isNA", "notNA")
Gwang-Jin Kim
  • 7,967
  • 11
  • 26