对于一个数据帧中的每个观测值,计算第二个数据帧中的观测值数量 [英] For each observation in one data frame, count number of observations in a second data frame

查看:52
本文介绍了对于一个数据帧中的每个观测值,计算第二个数据帧中的观测值数量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个数据帧。我想对第一个观察值进行所有观察,然后添加一列,以指示第二个数据框中符合特定条件的观察值。

I have two data frames. I would like to run on all of the observations from the first one, and add a column indicating the number of observations from the second data frame that fit a certain condition.

例如:

DF1                               
observation Value              
1            3
2            5
3            8
4            10
5            1

DF2                               
observation Value              
1            8
2            9
3            2
4            1
5            3

我想为DF1创建第三列,指示DF2中具有(例如)最大值为2或更大(| Value2-Value1 |< == 2)的观测值的数量。因此,在这种情况下,我的结果将是:

I want to create a third column for DF1, indicating the number of observations in DF2 that have a value (for example) that is at most greater or smaller by 2 (|Value2 - Value1| <= 2). Thus my result would be in this case:

DF1                               
observation Value  Count              
1            3       3
2            5       1
3            8       2
4            10      2
5            1       3


推荐答案

我们可以遍历'DF1'的'Value',获得其绝对差额的 sum 将是小于或等于2的'DF2'中的'值'

We can loop over the 'Value' of 'DF1', get the sum of the absolute difference of it will be the 'Value' from 'DF2' that is less than or equal to 2

DF1$Count <- sapply(DF1$Value, function(x) sum(abs(x-DF2$Value) <=2))






或者使用外部,我们从数据集中获得值列的每种组合的差,检查绝对值是否小于或等于2,并找到 rowSums


Or with outer, we get the difference of each of the combinations of the 'Value' columns from the datasets, check whether the absolute value is less than or equal to 2 and find the rowSums

DF1$Count <- rowSums(abs(outer(DF1$Value, DF2$Value, `-`))<=2)
DF1$Count
#[1] 3 1 2 2 3

这篇关于对于一个数据帧中的每个观测值,计算第二个数据帧中的观测值数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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