How to order by class and by descending order?

Asked

Viewed 71 times

2

Suppose the following database:

set.seed(1)

df_1 <- data.frame(x = replicate(n = 3, expr = sample(x = 1:3, size = 10, replace = TRUE)))

By the way, with the package dplyr I tried to:

library(dplyr)

df_1 %>% 
  arrange_if(.predicate = desc(is.integer))

Error in x[! nas] : Object of type 'builtin' is not subsettable

In addition: Warning message:

In is.na(x) is.na() Applied to non-(list or vector) of type 'builtin'

With arrange, arrange_all and arrange_at, desc works.

  • How to adjust arrange_if in descending order?

1 answer

2


Well, the point is that you used two arguments at once. The .predicate, which only exists in functions with _if is to select variables, so it must have the function that selects them, in case is.integer. And the .funs applies a function, in the case of desc.

set.seed(1)

df_1 <- data.frame(x = replicate(n = 3, expr = sample(x = 1:3, size = 10, replace = TRUE)))

library(dplyr)

df_1
#>    x.1 x.2 x.3
#> 1    1   3   3
#> 2    3   1   1
#> 3    1   1   1
#> 4    2   1   1
#> 5    1   2   1
#> 6    3   2   2
#> 7    3   2   1
#> 8    2   2   1
#> 9    2   3   2
#> 10   3   1   2

df_1 %>% 
  arrange_if(.predicate = is.integer, .funs = desc)
#>    x.1 x.2 x.3
#> 1    3   2   2
#> 2    3   2   1
#> 3    3   1   2
#> 4    3   1   1
#> 5    2   3   2
#> 6    2   2   1
#> 7    2   1   1
#> 8    1   3   3
#> 9    1   2   1
#> 10   1   1   1

Created on 2020-05-07 by the reprex package (v0.3.0)

  • 1

    The syntax of these functions with _if, _at and _all is kind of confusing. In the next version of dply will have a function across that replaces them and is more standardized for use with other functions such as mutate and summarize.

  • 1

    Thanks for the tip, @Jorge. Following it, I checked the function across is already available. To whom interested, just rotate: remotes::install_github("tidyverse/dplyr")

Browser other questions tagged

You are not signed in. Login or sign up in order to post.