How to Sort Data by Column in Descending Order in R

How to sort data by column in descending order in R

You need to use dataframe name as prefix

chickens[order(chickens$feathers),]  

To change the order, the function has decreasing argument

chickens[order(chickens$feathers, decreasing = TRUE),]  

How to sort data in descending order based on every second value in R?

One approach using a join:

df %>%
filter(year == 2021) %>%
# group_by(person) %>% slice(2) %>% ungroup() %>% #each person's yr2
arrange(-cash) %>%
select(-cash, -year) %>%
left_join(df)

Output:

       person year cash
1 personthree 2020 62
2 personthree 2021 55
3 personone 2020 29
4 personone 2021 40
5 persontwo 2020 17
6 persontwo 2021 13

R programming, how do I sort data frame by string column in descending order?

We can use decreasing = TRUE

temp[order(as.character(temp$y), decreasing = TRUE),]

How to order a data frame by one descending and one ascending column?

I used this code to produce your desired output. Is this what you were after?

rum <- read.table(textConnection("P1  P2  P3  T1  T2  T3  I1  I2
2 3 5 52 43 61 6 b
6 4 3 72 NA 59 1 a
1 5 6 55 48 60 6 f
2 4 4 65 64 58 2 b"), header = TRUE)
rum$I2 <- as.character(rum$I2)
rum[order(rum$I1, rev(rum$I2), decreasing = TRUE), ]

P1 P2 P3 T1 T2 T3 I1 I2
1 2 3 5 52 43 61 6 b
3 1 5 6 55 48 60 6 f
4 2 4 4 65 64 58 2 b
2 6 4 3 72 NA 59 1 a

Arranging summarized NAs in descending order after calculating them in each column in tidyverse

I think the issue is caused by the summarise function returning a single column ("col_name : value") instead of two columns ("col_name" and "variable"). One potential solution is to use the pivot_longer() tidyverse function to split the output into two columns, e.g.

library(tidyverse)

data(airquality)
dat1 <- airquality
dat1 %>%
summarise(across(everything(), ~ sum(is.na(.)))) %>%
pivot_longer(cols = everything(), names_to = "names", values_to = "values") %>%
arrange(desc(values))
#> # A tibble: 6 x 2
#> names values
#> <chr> <int>
#> 1 Ozone 37
#> 2 Solar.R 7
#> 3 Wind 0
#> 4 Temp 0
#> 5 Month 0
#> 6 Day 0

Created on 2021-07-21 by the reprex package (v2.0.0)

R data.table - How specify by variable in descending order?

We can first order the data and then assign the flag as required.

library(data.table)

dt <- mtcars
setDT(dt)
dt[, temp := 1]
dt1 <- dt[order(cyl,-mpg)]
dt1[, first_cyl := seq_len(.N) == which.max(temp), cyl]

# mpg cyl disp hp drat wt qsec vs am gear carb temp first_cyl
# 1: 33.9 4 71.1 65 4.22 1.835 19.90 1 1 4 1 1 TRUE
# 2: 32.4 4 78.7 66 4.08 2.200 19.47 1 1 4 1 1 FALSE
# 3: 30.4 4 75.7 52 4.93 1.615 18.52 1 1 4 2 1 FALSE
# 4: 30.4 4 95.1 113 3.77 1.513 16.90 1 1 5 2 1 FALSE
# 5: 27.3 4 79.0 66 4.08 1.935 18.90 1 1 4 1 1 FALSE
# 6: 26.0 4 120.3 91 4.43 2.140 16.70 0 1 5 2 1 FALSE
# 7: 24.4 4 146.7 62 3.69 3.190 20.00 1 0 4 2 1 FALSE
# 8: 22.8 4 108.0 93 3.85 2.320 18.61 1 1 4 1 1 FALSE
# 9: 22.8 4 140.8 95 3.92 3.150 22.90 1 0 4 2 1 FALSE
#10: 21.5 4 120.1 97 3.70 2.465 20.01 1 0 3 1 1 FALSE
#11: 21.4 4 121.0 109 4.11 2.780 18.60 1 1 4 2 1 FALSE
#12: 21.4 6 258.0 110 3.08 3.215 19.44 1 0 3 1 1 TRUE
#13: 21.0 6 160.0 110 3.90 2.620 16.46 0 1 4 4 1 FALSE
#....
#....

This assigns TRUE to the first row where temp = 1 for each cyl.

Sort (order) data frame rows by multiple columns

You can use the order() function directly without resorting to add-on tools -- see this simpler answer which uses a trick right from the top of the example(order) code:

R> dd[with(dd, order(-z, b)), ]
b x y z
4 Low C 9 2
2 Med D 3 1
1 Hi A 8 1
3 Hi A 9 1

Edit some 2+ years later: It was just asked how to do this by column index. The answer is to simply pass the desired sorting column(s) to the order() function:

R> dd[order(-dd[,4], dd[,1]), ]
b x y z
4 Low C 9 2
2 Med D 3 1
1 Hi A 8 1
3 Hi A 9 1
R>

rather than using the name of the column (and with() for easier/more direct access).

Using awk and sort last column in descending order in Linux

First, assuming there are really not blank lined in between each line of data in data.csv, all you need is sort, you don't need awk at all. For example, since there is only ':' before the total score you want to sort descending by, you can use:

sort -t: -k2,2rn data.csv

Where -t: tells sort to use ':' as the field separator and then the KEYDEF -k2,2rn tell sort to use the 2nd field (what's after the ':' to sort by) and the rn says use a reverse numeric sort on that field.

Example Use/Output

With your data (without blank lines) in data.csv, you would have:

$ sort -t: -k2,2rn data.csv
2014,Honda,Accord,83 Total score:112
1998,Subaru,Legacy,23 Total score:62
2008,Honda,Accord,88 Total score:48
2012,Volkswagen,Golf,59 Total score:28
2016,Bmw,M2,2 Total score:24
2001,Dodge,Viper,42 Total score:8
2015,Chevy,Camaro,0 Total score:0

Which is your sort by Total score in descending order. If you want ascending order, just remove the r from -k2,2rn.

If you do have blank lines, you can remove them before the sort with sed -i '/^$/d' data.csv.

Sorting by number Before "Total score"

If you want to sort by the number that begins the XX Total score: yy field (e.g. XX), you can use sort with the field separator being a ',' and then your KEYDEF would be -k4.1,4.3rn which just says sort using the 4th field character 1 through character 3 by the same reverse numeric, e.g.

sort -t, -k4.1,4.3rn data.csv

Example Use/Output

In this case, sorting by the number before Total score in descending order would result in:

$ sort -t, -k4.1,4.3rn data.csv
2008,Honda,Accord,88 Total score:48
2014,Honda,Accord,83 Total score:112
2012,Volkswagen,Golf,59 Total score:28
2001,Dodge,Viper,42 Total score:8
1998,Subaru,Legacy,23 Total score:62
2016,Bmw,M2,2 Total score:24
2015,Chevy,Camaro,0 Total score:0

After posting the original solution I noticed it was ambiguous as which of the numbers on the 4th field you intended to sort on. In either case, here are both solutions. Let me know if you have further questions.

sort dataframe r by column values

Substituting "DF" as your data frame, you can use this line of code:

DF[order(DF$new), ]

If you just wanted to view the data frame sorted, you can use

View(DF)

And click the arrows on the column name, which automatically sorts the data frame by ascending/descending.



Related Topics



Leave a reply



Submit