【发布时间】:2019-10-25 07:51:59
【问题描述】:
我正在尝试使用 dplyr 按变量进行分组,并确定我数据集中每个位置的最近位置。我还想包括尚未测量距离(NA)的所有行。
# Set up df of place, distance, and destination.
df <- data.frame(place = c('A','B','B','C','C','D','D'),dist = c(NA, 4, 1, 6, 3, 1, 1), dest = 1:7)
# For each place, get the nearest destination.
df %>%
group_by(place) %>%
top_n(1, desc(dist))
# This does not return a row for place A.
是否有使用 top_n 来识别基于排名的行的 tidyr 解决方案,其中还包括未排名的行?提前谢谢你。
【问题讨论】:
-
在某些情况下,
place的dist的值可能 > 1,其中一些值为 NA,而另一些则不是?如果是这样,这些情况应该返回什么?