3

I have a data frame like the one below:

df <- data.frame(v1 = c("A", "B", "A", "A", "B", "B", "B", "B", "A", "A", "A", "A"),
                 v2 = c("X", "Y", "X", "Y", "Z", "X", "X", "Y", "X", "Y", "Z", "Z"),
                 v3 = c(2, 1, 3, 1, 1, 2, 1, 2, 1, 2, 2, 1))

In this data frame v1 and v2 are so called grouping variables (charachter vectors is this case) within I'd like to order my counter variable v3 ascending using (a) base R function(s). There's no requirement for the order in which the grouping variables are sorted (both ascending and descending would be ok). Now in this special case that would be easy:

df <- df[order(df$v1, df$v2, df$v3),]

Or alternatively:

df <- df[do.call(what = order, args = df),]

What I'd like is a more general solution for any data frame with n grouping variables of which the names are contained in a vector and the name of the counter variable is contained in another vector. Reason I want this is that this data is given in a function call in a user defined function and can therefore vary.

grouping_vars <- c("v1", "v2", ..., "vn") #not actual code. Data frame contains *n* variables.
counter       <- "vi"                     #not actual code. One of them, the i-th, is the counter variable.

Again, I'd like to make use of a base R function here (most likely order) and not a solution from data.frame or tidyverse from example.

2
  • 2
    How about this df[do.call(what = order, args = df[c(grouping_vars, counter)]),] Commented May 11, 2020 at 13:44
  • Great! Can't believe I was almost there, but missed the last part. Thanks Darren Tsai. Can you formally answer the question with, so that you'll get the credits for it? To be completely clear, I'd like it in this format, writing an explicit comma in front of c(grouping_vars, counter): df_complete[do.call(what = order, args = df[,c(grouping_vars, counter)]),] Commented May 11, 2020 at 14:00

1 Answer 1

2

Your code is almost there. Just use [] behind df to extract grouping and numerical columns for ordering.

df[do.call(what = order, args = df[,c(grouping_vars, counter)]), ]

PeterD: I added a comma in front of the vector that contains the selected columns to be explicit about the selection of columns of data frame df.

Sign up to request clarification or add additional context in comments.

2 Comments

This is obviously four years old, but how would I do this if I wanted to sort by the absolute value of each vector?
You can convert the data used for sorting into absolute values. E.g. args = abs(df[...])

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.