[英]How do I use dplyr::arrange to sort NA's first?
I'd like to sort flights
in ascending order of dep_time
with NA
s first using dplyr
's arrange
in dplyr_0.8.0
. 我想排序
flights
在升序dep_time
与NA
第一个使用dplyr
的arrange
在dplyr_0.8.0
。 arrange
's default is to list NA
s last. arrange
的默认是列出NA
的最后。
I had thought that 我以为
arrange(flights,desc(is.na(dep_time)),dep_time)
would work but NA
s still come last. 可以工作,但
NA
仍然排在最后。 In fact, both 实际上,两者
desc(is.na(dep_time))
and 和
is.na(dep_time)
produce the same arrangement. 产生相同的安排。 Why is this and how do I get the desired sort?
为什么会这样,如何获得所需的排序?
Edit: here's a minimal, reproducible example. 编辑:这是一个最小的,可复制的示例。
library(tidyverse)
set.seed(1)
df <- tibble(x = sample(c(NA,NA,1:4)))
arrange(df,desc(is.na(x)),x)
arrange(df,is.na(x),x)
Here's the output. 这是输出。
...
> arrange(df,desc(is.na(x)),x)
# A tibble: 6 x 1
x
<int>
1 1
2 2
3 3
4 4
5 NA
6 NA
> arrange(df,is.na(x),x)
# A tibble: 6 x 1
x
<int>
1 1
2 2
3 3
4 4
5 NA
6 NA
It works as expected if I mutate(ind = is.na(x))
and then sort on the variable ind
rather than the expression is.na(x)
. 如果我
mutate(ind = is.na(x))
然后对变量ind
而不是表达式is.na(x)
进行排序,它将按预期工作。 Here's my sessionInfo() . 这是我的sessionInfo() 。 All hints toward solution gratefully received.
收到所有有关解决方案的提示。
通过下载最新版本的dplyr_0.8.0
:
devtools::install_github("tidyverse/dplyr")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.