问题描述
library(dplyr)
a <- data_frame(id = c("A","A","A","B","B","B"),
b = c(1.2, 1.5, 1.8, 1.1, 1.6, 1.4))
现在,我想为id中的每个类别检索最接近1.43的值。
我想我可以使用:
Now, I´d like to retrieve the values closest to 1.43 for each of the catergories in id.I thought I could use:
a %>% group_by(id) %>% nth(which.min(abs(.$b-1.43)))
但dplyr声明
Error: Don't know how to generate default for object of class grouped_df/tbl_df/tbl/data.frame
推荐答案
which.min()
返回数值(或逻辑)向量的(第一个)最小值或最大值的索引。如果有多个相等的值,彼此之间的距离接近1.43,并且您想保留所有这些值,则可以使用 filter()
:
which.min()
returns the index of the (first) minimum or maximum of a numeric (or logical) vector. If there are multiple equal values as close to 1.43 as each other and you want to keep all of them, you can use filter()
:
a %>% group_by(id) %>% filter(abs(b - 1.43) == min(abs(b - 1.43)))
#Source: local data frame [2 x 2]
#Groups: id [2]
# id b
# <chr> <dbl>
#1 A 1.5
#2 B 1.4
如果您喜欢使用 nth()
函数,并且每个组只有一个值是可以的,则可以将其包装在摘要中函数,以便将其应用于每个组,并且根据?nth()
,还需要将向量作为参数传递给函数:
If you prefer sticking with the nth()
function, and it is OK to have only one value for each group, you can wrap it within a summarize function so that it will be applied to each group, and also according to ?nth()
, you need to pass the vector to the function as an argument as well:
a %>% group_by(id) %>% summarise(b = nth(b, which.min(abs(b-1.43))))
# A tibble: 2 × 2
# id b
# <chr> <dbl>
#1 A 1.5
#2 B 1.4
这篇关于在dplyr中按组查找最接近x的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!