如何比较R中两个数据框的行 [英] How to compare the rows of two dataframes in R

查看:55
本文介绍了如何比较R中两个数据框的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试比较两列不同的数据框架以创建一个新的数据框架.如果第一个col的行的值小于第二个col的行,它将在新列中添加1.值较大时,将添加2,依此类推.

I'm trying to compare two columns of different data frames to create a new data frame. If the value of the row of the first col is less than the second, it will add a 1 to the new column. When the value is greater, it will add a 2 and so on.

我给你举个例子.我有这个df

I'll give you an example. I have this df

df1 <- data.frame(col=c(1,seq(1:9),9,10))
# col
# 1    1
# 2    1
# 3    2
# 4    3
# 5    4
# 6    5
# 7    6
# 8    7
# 9    8
# 10   9
# 11   9
# 12  10

这是行数较少的

df2<-data.frame(col2=c(3,6,8))
#    col2
# 1    3
# 2    6
# 3    8

现在,我的愿望输出将类似于以下内容:

Now, my desire output would be something similar to this:

#      col3
# 1     1
# 2     1
# 3     1
# 4     2
# 5     2
# 6     2
# 7     3
# 8     3
# 9     4
# 10    4
# 11    4
# 12    4

我知道这是一个非常基本的问题,但是如果不使用for循环,我将无法轻松地做到这一点.我虽然要使用!unique()来选择第一个元素,然后使用%in%来查看它是否在第二个元素中,但是不知道如何实现它.

I know this is a very basic question, but I'm not getting how to do this easily withouth using a for loop. I though about using !unique() to select the first element and see if its in the second with %in%but don't know how to implement it.

推荐答案

如果我对您的理解正确,我认为这会起作用:

If I understand you correctly, I think this would work:

apply(df1, 1, FUN = function(x) 1 + sum(x >= df2$col2))
# [1] 1 1 1 2 2 2 3 3 4 4 4 4

我们使用 apply 遍历 df1 的行,然后检查每行中的值以查看其与 col2 的比较在 df2 中.

We use apply to iterate over the rows of df1, and then check the value in each row to see how it compares to col2 in df2.

一个dplyr替代方案:

A dplyr alternative:

library(dplyr)
df1 %>%
    rowwise() %>% # group over each row
    mutate(col3 = 1 + sum(col >= df2$col2))

     col  col3
   <dbl> <dbl>
 1     1     1
 2     1     1
 3     2     1
 4     3     2
 5     4     2
 6     5     2
 7     6     3
 8     7     3
 9     8     4
10     9     4
11     9     4
12    10     4

这篇关于如何比较R中两个数据框的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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