0

I am stuck in one problem and try to find the similar solution but was unable to find that here in stackoverflow. My data look like this:

x1 <- data.frame(x = c(1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0))

y1 <- data.frame(x = c(sin(x1 - 3.4)), y = c(cos(x1-3.2), z = tan(x1-3.5)))

What I want to do is to replace the value of y1 by NA if x1 = 0 but would like to keep the value as it is if x1 = 1 for whole data frame y1 at the same time. I

I tried ifelse function as

y1 <- ifelse(x1 == 0, NA, y1)

But I was unable to process that. If anyone can suggest me the answer without loop that would be great.

Thanks in advance

5
  • 2
    y1[x1==0,]=NA Commented Oct 18, 2017 at 2:46
  • Try (NA^!x1)*y1 Commented Oct 18, 2017 at 2:55
  • There are multiple columns in your "y1" data frame. Clarify whether you'd like a particular column or single column from "y1" to be replaced with NA when applicable. Commented Oct 18, 2017 at 4:15
  • Hi Dale Kube I have multiple column and all the column of y1 should be replaced with NA Commented Oct 18, 2017 at 18:43
  • I got the answer thank you everyone Commented Oct 18, 2017 at 18:49

1 Answer 1

2
y1[x1$x == 0,] <- NA

Note that this sets relevant rows in all columns in y1 as NA. You could do this for one column only by, for example:

y1$x[x1$x == 0] <- NA
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very very much for the answer.

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.