简体   繁体   English

如果另一个列值与 R 中另一个数据框中的相应单元格不匹配,则将数据框中的列值替换为 null

[英]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.

相关问题 替换与R中另一个data.frame的多个列匹配的列中的值 - Replacing values in a column that match multiple columns of another data.frame in R 从数据框中删除其列值与另一个数据框的列值不匹配的数据 - R - remove rows from data frame whose column values don't match another data frame's column values - R 根据R中另一列的值乘以数据框列的值 - Multiplying data frame column values based on the value of another column in R R根据另一个数据框的精确匹配替换列的值 - R replace values of a column based on exact match of another data frame 在R中:当条件匹配时,用另一个数据帧的值替换数据帧列的值 - In R: Replacing value of a data frame column by the value of another data frame when between condition is matched 比较数据框中某列中的元素与另一数据框中同一列中的另一个元素以获取R中的对应行 - comparing an element in a column in a data frame with another element in the same column in another data frame for corresponding rows in R 基于另一列替换数据框列中的值 - Replacing values in data frame column based on another column R:从数据框中的一列返回一个值,该值对应于另一列中的最小值 - R: return a value from one column in a data frame corresponding to the minimum value in another column 如何在R中的数据帧中找到一列中出现字符串最长的时间以及另一列中对应的第一个和最后一个值? - How to find the longest occurrence of a string in a column and corresponding first and last values from another column in a data frame in R? 使用另一个数据框中的唯一值和分配给列的相应值创建具有列名的新数据框 - Create New Data Frame with Column Names from Unique Values in another Data Frame and Corresponding Values Assigned to Column
相关标签
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM