[英]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.我对 R 很陌生,并试图找出一种方法来比较两个不同数据框中的日期,如果 df1 中的日期与 df2 中的相应单元格不同,则将 df1 中该日期的值替换为 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!
谢谢!
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.