Arrange alphanumeric

I have a draft.status column with c(d-2, d-1, d, d+1, d+2, d+3) but when i use the code below I get an arranged column of d+3, d+2, d+1, d-1, d-2, d. I would like the arranged column to be: d+3, d+2, d+1, d, d-1, d-2. Any help is appreciated....

#average ppg by draft status
nhl.draft <- ohl %>%
  group_by(draft.status, position) %>%
  summarise(mean = mean(ohl.ppg, na.rm = TRUE)) %>%
  filter(position == "F") %>%
  mutate_at(3, funs(round(., 2))) %>%
  arrange(desc(draft.status))

Could you provide sample data on a copy/paste friendly format to make this reproducible? here is a nice blog post by Mara that shows how to do it.

hope this works....

rstudio <- tibble::tribble(
             ~draft.status, ~position, ~mean,
                     "d+3", "forward",  0.84,
                     "d+2", "forward",  0.82,
                     "d+1", "forward",   0.6,
                     "d-2", "forward",  0.23,
                     "d-1", "forward",  0.29,
                       "d", "forward",  0.43
             )
head(rstudio)
#> # A tibble: 6 x 3
#>   draft.status position  mean
#>   <chr>        <chr>    <dbl>
#> 1 d+3          forward  0.84 
#> 2 d+2          forward  0.82 
#> 3 d+1          forward  0.6  
#> 4 d-2          forward  0.23 
#> 5 d-1          forward  0.290
#> 6 d            forward  0.43

Created on 2019-07-25 by the reprex package (v0.3.0)

Is this what you mean?

rstudio <- tibble::tribble(
    ~draft.status, ~position, ~mean,
    "d+3", "forward",  0.84,
    "d+2", "forward",  0.82,
    "d+1", "forward",   0.6,
    "d-2", "forward",  0.23,
    "d-1", "forward",  0.29,
    "d", "forward",  0.43
)

library(dplyr)

rstudio %>% 
    mutate(draft.status = factor(draft.status,
                                 levels = c("d-2", "d-1", "d", "d+1", "d+2", "d+3"))) %>% 
    arrange(desc(draft.status))
#> # A tibble: 6 x 3
#>   draft.status position  mean
#>   <fct>        <chr>    <dbl>
#> 1 d+3          forward  0.84 
#> 2 d+2          forward  0.82 
#> 3 d+1          forward  0.6  
#> 4 d            forward  0.43 
#> 5 d-1          forward  0.290
#> 6 d-2          forward  0.23

Created on 2019-07-25 by the reprex package (v0.3.0.9000)

No.

I have a tibble that has multiple columns that I group_by, summarise, and then filter (my code is in the original post) and then I want to arrange in the order of:

d+3
d+2
d+1
d
d-1
d-2

The code in the original post will arrange in:
d+3
d+2
d+1
d-2
d-1
d

actually that works if I use an ungroup after your mutate code.

Thank you very much!

Your sample data wasn't grouped so I had no way to know that in advance, to avoid this kind of issues next time make your question with a self-contained REPRoducible EXample (reprex)

If your question's been answered, would you mind choosing a solution? It helps other people see which questions still need help, or find solutions if they have similar problems. Here’s how to do it:

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.