0

I am using the Tidyverse package in R. I have a data frame with 20 rows and 500 columns. I want to sort all the columns based on the size of the value in the last row of each column.

Here is an example with just 3 rows and 4 columns:

1 2 3 4,
5 6 7 8,
8 7 9 1

The desired result is:

3 1 2 4,
7 5 6 8,
9 8 7 1

I searched stack overflow but could not find an answer to this type of question.

1
  • You might find a general solution will return unexpected results when one of your columns is character, causing all columns to be cast as strings, resulting in an alphabetic sort, not a numeric sort. Commented Mar 6, 2019 at 1:26

2 Answers 2

1

If we want to use dplyr from tidyverse, we can use slice to get the last row and then use order in decreasing order to subset columns.

library(dplyr)

df[df %>% slice(n()) %>% order(decreasing = TRUE)]

#  V3 V1 V2 V4
#1  3  1  2  4
#2  7  5  6  8
#3  9  8  7  1

Whose translation in base R would be

df[order(df[nrow(df), ], decreasing = TRUE)]

data

df <- read.table(text = "1 2 3 4
                         5 6 7 8
                         8 7 9 1")
Sign up to request clarification or add additional context in comments.

1 Comment

The two solutions did not work in my particular application the first time I tried them, but I found that when I changed the class of my data frame to "matrix" both solutions did work. Much appreciated.
1

The following reorders the data frame columns by the order of the last-rows values:

df <- data.frame(col1=c(1,5,8),col2=c(2,6,7),col3=c(3,7,9),col4=c(4,8,1))
last_row <- df[nrow(df),]
df <- df[,order(last_row,decreasing = T)]

First, to get the last rows. Then to sort them with the order() function and to return the reordered columns.

>df
  col3 col1 col2 col4
1    3    1    2    4
2    7    5    6    8
3    9    8    7    1

3 Comments

I see an issue in the code; may be due to this. There is a warning coming up as In xtfrm.data.frame(x) : cannot xtfrm data frames. See, https://www.r-bloggers.com/2021/02/it-has-always-been-wrong-to-call-order-on-a-data-frame/.
The solution runs without warning or error on my end, using R4.0.2. The link in your comment discusses doing order(df); that is, passing the entire data.frame as an argument to order(). That is not what's happening here. Rather, all columns of the last row are being selected. Then the columns are reordered, with a one-dimensional vector being passed to order().
I exactly copied your code and ran on Ubuntu with R 4.1.0 and it produced error. However, when I converted the data.frame to matrix then this did not appear. Logically, your point is agreed, but what I am getting it remains an issue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.