user3356873
user3356873

Reputation: 63

Using condition to replace value of data frame using another data frame in R

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

Upvotes: 0

Views: 56

Answers (1)

bhlmn
bhlmn

Reputation: 66

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

Upvotes: 2

Related Questions