Home > Mobile >  how to sort in ascending order all columns of a data frame in r?
how to sort in ascending order all columns of a data frame in r?

Time:09-15

I have a data frame with 348 numeric columns and I would like to put all these columns in ascending order. How to do this? I tried some codes that sorted the first column in ascending order. I want them all in ascending order. Exemple:

a1 = runif(n = 50, min = 0.1, max = 120)
a2 = runif(n = 50, min = 5, max = 151)
a3 = runif(n = 50, min = 1, max = 100)
a4 = runif(n = 50, min = 6, max = 180)
a5 = runif(n = 50, min = 6, max = 183)
a6 = runif(n = 50, min = 6, max = 254)

df = data.frame(a1,a2,a3,a4,a5,a6)
df

df2 = df[with(df, order(a1,a2,a3,a4,a5,a6)), ]
head(df2)

df2 with the first column in ascending order but I want them all (a1:a6 in ascending order)

CodePudding user response:

library(tidyverse)

df %>% 
  mutate(across(everything(), sort))

# A tibble: 50 x 6
      a1    a2    a3    a4    a5    a6
   <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
 1  2.30  5.31  3.28  8.25  8.62  11.2
 2  3.39 15.6   3.70 16.4  21.0   12.7
 3  7.11 24.3   8.29 17.9  22.1   17.4
 4 10.8  24.3   9.09 25.3  25.8   22.0
 5 11.9  40.2   9.86 25.4  28.7   24.3
 6 13.7  40.6  11.3  25.7  31.3   37.1
 7 21.4  41.6  12.3  29.6  39.1   38.1
 8 27.1  56.3  12.8  39.3  40.5   45.5
 9 28.7  60.1  14.3  39.8  46.2   63.8
10 29.1  62.1  15.4  50.2  46.8   72.8
# ... with 40 more rows
# i Use `print(n = ...)` to see more rows

CodePudding user response:

Using data.table should be fast:

library(data.table)
setDT(df)

df[, names(df) := lapply(.SD, sort)]
head(df)
          a1        a2        a3       a4       a5        a6
1: 0.1877075  6.731233  1.354166 12.82542 15.43206  8.385348
2: 0.3087580 14.567764  7.271257 14.18342 18.84559 20.998163
3: 1.3518180 15.943932  8.163164 15.69317 20.46418 28.797276
4: 6.9881726 19.086500 11.085330 18.38545 29.16079 30.808385
5: 7.5015324 19.158892 12.257267 18.86634 32.27948 32.997170
6: 7.6574802 21.143694 14.037592 20.07155 37.85144 49.052889

Note that this is updating by reference, and so there is no need to write something like df <- df %>% ... in this situation.

CodePudding user response:

Use sapply sort. Using order on multiple columns, you will not be able to change the row positions of the columns independently, i.e. you'll get an order of priority for columns to be sorted.

sapply(df, sort)
  • Related