R基于平等以外的条件合并 [英] R merge based on condition other than equality

查看:111
本文介绍了R基于平等以外的条件合并的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据框看起来像:

I have a dataframe that looks something like:

date            minutes_since_midnight   value
2015-01-01      50                       2
2015-01-01      60                       1.5
2015-01-02      45                       3.3
2015-01-03      99                       5.5

另一个数据框看起来像这样

and another dataframe looking something like this

date        minutes_since_midnight   other_value
2015-01-01  55                       12
2015-01-01  80                       33
2015-01-02  45                       88

我想做的是在第一个数据帧中添加另一个列,即布尔值值,第二个数据帧中是否存在一个相等值的行日期列,然后是 minutes_since_midnight 小于或等于 minutes_since_midnight 从第一个数据帧。所以对于上面的例子数据我们会得到:

What I want to do is add another column to the first data frame, which is the boolean value whether a row exists in the second data frame for an equal value in the date column and then a minutes_since_midnight which is less than or equal to the minutes_since_midnight from the first data frame. So for the above example data we'd get:

date        minutes_since_midnight    value  has_other_value
2015-01-01  50                        2      False
2015-01-01  60                        1.5    True
2015-01-02  45                        3.3    True
2015-01-03  99                        5.5    False

我该怎么做?

希望这是有道理的,

提前感谢

推荐答案

您可以将变量 minutes_since_midnight 重命名为 minutes_since_midnight1 minutes_since_midnight2 ,将两个数据帧合并在一起,然后使用if else语句创建所需的 has_other_value 变量。

Can you not rename the variables minutes_since_midnight to minutes_since_midnight1 and minutes_since_midnight2, merge the two data frames together then create the required has_other_value variable with an if else statement.

这篇关于R基于平等以外的条件合并的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆