1

I have a data frame with a group column and several numeric columns. In addition, I have a variable that stores the column name I need to rank observation on. Say this is my data frame

> x = "myValue1"
> set.seed(123)
> df = data.frame(myValue1 = rnorm(9, mean = 10),
                  myValue2 = rnorm(9, mean = 2),
                  myGroup = rep(c("a","b","c"), each = 3), 
                  myEntity = paste0("entity",1:9))
> df
   myValue1   myValue2 myGroup myEntity
1  9.439524 1.55433803       a  entity1
2  9.769823 3.22408180       a  entity2
3 11.558708 2.35981383       a  entity3
4 10.070508 2.40077145       b  entity4
5 10.129288 2.11068272       b  entity5
6 11.715065 1.44415887       b  entity6
7 10.460916 3.78691314       c  entity7
8  8.734939 2.49785048       c  entity8
9  9.313147 0.03338284       c  entity9

what I want is within each group to return the rank of each observation using the column from x (in this case it is myValue1). So, I want to return the following data frame

 > library(dplyr)
 > df = data.frame(df %>% 
                   group_by(myGroup) %>% 
                   mutate(myRank = order(myValue1,myEntity)))
 > df
       myValue1   myValue2 myGroup myEntity myRank
    1  9.439524 1.55433803       a  entity1      1
    2  9.769823 3.22408180       a  entity2      2
    3 11.558708 2.35981383       a  entity3      3
    4 10.070508 2.40077145       b  entity4      1
    5 10.129288 2.11068272       b  entity5      2
    6 11.715065 1.44415887       b  entity6      3
    7 10.460916 3.78691314       c  entity7      2
    8  8.734939 2.49785048       c  entity8      3
    9  9.313147 0.03338284       c  entity9      1

This was easy because I typed in the column name. My problem is how can I use the variable x instead of typing the column name in this formula. This is what I tried without success (they all return errors)

> df = data.frame(df %>% group_by(myGroup) %>% mutate(myRank = order(x,myEntity)))
> df = data.frame(df %>% group_by(myGroup) %>% mutate(myRank = order(as.name(x),myEntity)))
> df = data.frame(df %>% group_by(myGroup) %>% mutate(myRank = order(noquote(x),myEntity)))
> library(plyr)
> df = ddply(df,.(myGroup),transform,Order = rank(as.name(x)))

I also looked at the question here similar question but couldn't make it work for my scenario.

lmo
  • 37,904
  • 9
  • 56
  • 69
BICube
  • 4,451
  • 1
  • 23
  • 44

1 Answers1

1

You could use the .data pronoun to refer to variables with strings:

x <- "myValue1"

df %>% 
  group_by(myGroup) %>% 
  mutate(myRank = order(.data[[x]], myEntity))

Or use sym() from rlang with the unquote operator UQ():

df %>% 
  group_by(myGroup) %>% 
  mutate(myRank = order(UQ(rlang::sym(x)), myEntity))
Steven Beaupré
  • 21,343
  • 7
  • 57
  • 77