You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When sorting a data frame/tibble based on a character column, arrange uses a different sort order than what is used by sort and by most (all?) spreadsheet programs. This creates issues when working on data coming from/going to a spreadsheet. Interestingly, use of the desc function within arrange switches the sort order to conform to sort and the spreadsheets.
# Demonstrate sorting discrepancy between `arrange` and `sort`.# Create sample data. The second column is just to ensure that sorting does not# convert a data frame into a vector.df<-data.frame(Label= c("bama", "mama", "1000x", "BAnn", "10:00x"), Index=1:5)
# Sort the rows into ascending label order using `dplyr::arrange`.df|>dplyr::arrange(Label) |> print()
#> Label Index#> 1 1000x 3#> 2 10:00x 5#> 3 BAnn 4#> 4 bama 1#> 5 mama 2
# Sort the rows into ascending label order using `sort`.df[sort(df$Label, index.return=TRUE)$ix, ] |> print()
#> Label Index#> 5 10:00x 5#> 3 1000x 3#> 1 bama 1#> 4 BAnn 4#> 2 mama 2
# Sort with `arrange` in "not descending" order.df|>dplyr::arrange(-dplyr::desc(Label)) |> print()
#> Label Index#> 1 10:00x 5#> 2 1000x 3#> 3 bama 1#> 4 BAnn 4#> 5 mama 2
The text was updated successfully, but these errors were encountered:
Thanks. I wondered if locale was an issue, but failed to read the fine print. (I assume it would use the operating system's default locale.) It's interesting that arrange defaults to the C locale but desc apparently does not.
When sorting a data frame/tibble based on a character column,
arrange
uses a different sort order than what is used bysort
and by most (all?) spreadsheet programs. This creates issues when working on data coming from/going to a spreadsheet. Interestingly, use of thedesc
function withinarrange
switches the sort order to conform tosort
and the spreadsheets.The text was updated successfully, but these errors were encountered: