yeetbro
yeetbro

Reputation: 45

Replacing column values with null in a data frame if another column value doesn't match with the corresponding cell in another data frame in R

I'm pretty new to R and was trying to figure out a way to compare dates in two different data frames and if a date in df1 is different from the corresponding cell in df2, then replacing the value for that date in df1 with NA.

Here's an example with two data frames to compare:

Value EventDate    Value EventDate
10    2018-07-10   14    2019-07-23
10    2017-09-12   28    2017-09-12
40    2018-02-28   15    2018-02-28

It should look like this after

Value EventDate    Value  EventDate
NA    2018-07-10   14     2019-07-23
10    2017-09-12   28     2017-09-12
40    2018-02-28   15     2018-02-28

I would appreciate any help. Thanks!

Upvotes: 0

Views: 119

Answers (1)

akrun
akrun

Reputation: 887118

Create a logical condition and replace the elements in 'Value' column based on that logical index

i1 <- df1$EventDate != df2$EventDate
df1$Value[i1] <- NA

Upvotes: 1

Related Questions