简体   繁体   中英

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 . arrange 's default is to list NA s last.

I had thought that

arrange(flights,desc(is.na(dep_time)),dep_time) 

would work but NA s still come last. 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) . Here's my sessionInfo() . All hints toward solution gratefully received.

通过下载最新版本的dplyr_0.8.0

devtools::install_github("tidyverse/dplyr")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM