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"
3
  • 2
    Can you make your problem reproducible and share dput(X$Planned)? Commented Oct 21, 2018 at 18:32
  • 1
    why not X$IsNA <- ifelse(is.na(X$Planned), "ISNA", "NOTNA") Commented Oct 21, 2018 at 18:34
  • I appreciate your support r2evans, this solved the issue Commented Oct 23, 2018 at 5:30

1 Answer 1

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")
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.