Reputation: 439
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.
Upvotes: 3
Views: 380
Reputation: 35554
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
.
Upvotes: 2