0

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,360
  • 10
  • 25
Ramon
  • 3
  • 5
  • 2
    Can you make your problem [reproducible](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) and share `dput(X$Planned)`? – markus Oct 21 '18 at 18:32
  • 1
    why not `X$IsNA <- ifelse(is.na(X$Planned), "ISNA", "NOTNA")` – r2evans Oct 21 '18 at 18:34
  • I appreciate your support r2evans, this solved the issue – Ramon Oct 23 '18 at 05:30

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
  • 9,303
  • 17
  • 30