Back

Explore Courses Blog Tutorials Interview Questions
0 votes
1 view
in R Programming by (5.3k points)

I wish to order a data frame based on different columns, one at a turn. I have a character vector with the relevant column names on which the order should be based:

parameter <- c("market_value_LOCAL", "ep", "book_price", "sales_price", "dividend_yield",

               "beta", "TOTAL_RATING_SCORE", "ENVIRONMENT", "SOCIAL", "GOVERNANCE")

I wish to loop over the names in parameter and dynamically select the column to be used to order my data:

Q1_R1000_parameter <- Q1_R1000[order(Q1_R1000$parameter[X]), ]

where X is 1:10 (because I have 10 items in parameter).

To make my example reproducible, consider the data set mtcars and some variable names stored in a character vector cols. When I try to select a variable from mtcars using a dynamic subset of cols, in a similar way as above (Q1_R1000$parameter[X]), the column is not selected:

cols <- c("cyl", "am")

mtcars$cols[1]

# NULL

1 Answer

0 votes
by

To dynamically select data frame columns, you can use the [ (or [ [ if you want to extract only a single column as a vector).

For example:

set.seed(123)

df <- data.frame( A = sample(5,10,replace = TRUE) , B = sample(5,10,replace = TRUE) , C = sample(5,10,replace = TRUE) )

   A B C

1  3 5 2

2  3 3 1

3  2 3 3

4  2 1 4

5  3 4 1

6  5 1 3

7  4 1 5

8  1 5 4

9  2 3 2

10 3 2 5

To order using do.call:

sort_by <- c("C","A")

df[ do.call( order , df[ , match( sort_by , names(df) ) ]  ) , ]

Output:

   A B C

2  3 3 1

5  3 4 1

9  2 3 2

1  3 5 2

3  2 3 3

6  5 1 3

8  1 5 4

4  2 1 4

10 3 2 5

7  4 1 5

In the above code, we have used  'do.call' to call order. The second argument in do.call is a list of arguments that we pass to the first argument, in this case 'order'. Since a data.frame is really a list, we just subset the data.frame according to the columns we want to sort in, in that order.

Welcome to Intellipaat Community. Get your technical queries answered by top developers!

28.4k questions

29.7k answers

500 comments

94.6k users

Browse Categories

...