0

Apologies if I have missed where this has been asked before - I couldn't find it. I am learning about tibbles and the verb arrange. I am wondering if there is a more efficient way to arrange rows in descending order of total row NA count then I have done below? I am using the nycflights13 dataset.

library(nycflights13)
library(tidyverse)

options(tibble.width = Inf)
flights$na_count <- flights %>% is.na %>% rowSums
arrange(flights, desc(na_count))

Part of result:

enter image description here

I checked the help centre and this appears to be on topic though I know working code is often a candidate for code review.

QHarr
  • 83,427
  • 12
  • 54
  • 101

1 Answers1

2

Not sure whether much more efficient, however, you can rewrite it into:

flights %>% 
 arrange(desc(rowSums(is.na(.))))
tmfmnk
  • 38,881
  • 4
  • 47
  • 67
  • 1
    Here's a base equivalent: ```flights[order(-xtfrm(rowSums(is.na(flights)))),]```. ```desc``` is equivalent to ```-xtfrm```. – Cole Aug 03 '19 at 19:06
  • Base R approaches can be found https://stackoverflow.com/questions/24093446/sort-data-by-number-of-nas-in-each-line too @Cole – Hector Haffenden Aug 03 '19 at 19:09
  • Thanks - note the link is for the default increasing. However, I forgot that ```order()``` has a ```decreasing``` argument: ```flights[order(rowSums(is.na(flights)), decreasing = T), ]``` – Cole Aug 03 '19 at 19:12
  • @Cole Thanks for the interesting additional info. – QHarr Aug 03 '19 at 19:21