R 根据另一列的值选择匹配项

R 根据另一列的值选择匹配项,r,dataframe,dplyr,R,Dataframe,Dplyr,我有一个带有公司匹配项的dataframe,我想提取某些行(匹配项) 我的意见 df <- data.frame(company_us= c("apple","google","netflix","apple","netflix","google"), company_eu = c("nokia","vodaf

我有一个带有公司匹配项的
dataframe
,我想提取某些行(匹配项)

我的意见

df <- data.frame(company_us=
c("apple","google","netflix","apple","netflix","google"),
                company_eu = c("nokia","vodafone","sky","sky","nokia","vodafone"),
                difference = c(5,5,5,10,10,10)
       )

#the df

company_us company_eu difference
1      apple      nokia          5
2     google   vodafone          5
3    netflix        sky          5
4      apple        sky         10
5    netflix      nokia         10
6     google   vodafone         10

我不知道如何解决这个问题。如果有人能推荐一些关于如何解决这样一个问题的文献或其他东西,我将不胜感激

使用
dplyr
可以使用函数检索组内变量的最小值

library(dplyr)

df %>% 
  group_by(company_us) %>% 
  slice_min(difference)

# A tibble: 3 x 3
# Groups:   company_us [3]
  company_us company_eu difference
  <chr>      <chr>           <dbl>
1 apple      nokia               5
2 google     vodafone            5
3 netflix    sky                 5
库(dplyr)
df%>%
集团(公司)%>%
最小切片数(差值)
#一个tibble:3x3
#组别:美国公司[3]
公司与美国公司的差异
1苹果诺基亚5
2谷歌沃达丰5
3 netflix sky 5

由于您似乎缺乏关于数据组内操作的一些知识,因此我给您留下一些
groupby()
函数的应用示例。

使用
ave
from
base R

df[with(df, difference == ave(difference, company_us, FUN = min)),]

A
data.table
选项使用'which.min`

> setDT(df)[, .SD[which.min(difference)], company_us]
   company_us company_eu difference
1:      apple      nokia          5
2:     google   vodafone          5
3:    netflix        sky          5

如果您创建一个小的可复制的示例以及预期的输出,那么会更容易提供帮助。了解。
> setDT(df)[, .SD[which.min(difference)], company_us]
   company_us company_eu difference
1:      apple      nokia          5
2:     google   vodafone          5
3:    netflix        sky          5