Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

arrange() doesn't recognize column name parameter

Tags:

r

dplyr

Within R, I use dplyr and more specifically arrange(). Somehow the arrange function doesn't work as expected.

In the example below first I store the name of a column, then I pass this variable as a parameter to a custom function called 'my_function'.

target_column = 'mean_age'

# below the function
my_function <- function(target_column, number){
    df <- read.csv('file.csv', stringsAsFactors=FALSE)
    df <- df[, c(1,4,10)]
    names(df) <-  c('place','state','mean_age')
    df1 <- df %>% group_by(state) %>% arrange(target_column) 
    df1 %>% summarise(rank = nth(target_column, number))        
}

R returns an error when 'my_function' is called due to the input to arrange():

"Error in arrange_impl(.data, dots) : incorrect size (1) at position 1, expecting : 4000"

When the name of the column is put directly into arrange(), instead of a variable that references to a string (like example above), it does accept the parameter.

df %>% group_by(state) %>% arrange(mean_age) 

How can I pass the parameter for the column name in a better way to 'my_function', so arrange() will recognize it?

like image 269
Elyakim Avatar asked Jan 03 '23 06:01

Elyakim


1 Answers

You need to first parse your string argument to a quosure, then unquote it with !!:

library(dplyr)
library(rlang)

target_column = 'mean_age'

my_function <- function(target_column, number){
    target_quo = parse_quosure(target_column)

    df <- read.csv('file.csv', stringsAsFactors=FALSE)
    df <- df[, c(1,4,10)]
    names(df) <-  c('place','state','mean_age')
    df1 <- df %>% group_by(state) %>% arrange(!!target_quo) 
    df1 %>% summarise(rank = nth(target_column, number))        
}

my_function('mean_age', 10)

If you want to be able to supply target_column as an unquoted column name, you can use enquo instead:

my_function <- function(target_column, number){
    target_quo = enquo(target_column)

    df <- read.csv('file.csv', stringsAsFactors=FALSE)
    df <- df[, c(1,4,10)]
    names(df) <-  c('place','state','mean_age')
    df1 <- df %>% group_by(state) %>% arrange(!!target_quo) 
    df1 %>% summarise(rank = nth(target_column, number))        
}

my_function(mean_age, 10)

Note:

Normally, enquo will also work for string arguments, but arrange itself does not allow it, so the following does not work for the second example:

my_function('mean_age', 10)

Below is a toy example to demonstrate what I mean, since OP's question is not reproducible:

library(dplyr)
library(rlang)

test_func = function(var){
    var_quo = parse_quosure(var)
    mtcars %>%
      select(!!var_quo) %>%
      arrange(!!var_quo)
}

test_func2 = function(var){
  var_quo = enquo(var)
  mtcars %>%
    select(!!var_quo) %>%
    arrange(!!var_quo)
}

Results:

> test_func("mpg") %>%
+   head()
   mpg
1 10.4
2 10.4
3 13.3
4 14.3
5 14.7
6 15.0

> test_func2(mpg) %>%
+   head()
   mpg
1 10.4
2 10.4
3 13.3
4 14.3
5 14.7
6 15.0

> test_func2("mpg") %>%
+   head()

Error in arrange_impl(.data, dots) : incorrect size (1) at position 1, expecting : 32

like image 121
acylam Avatar answered Jan 13 '23 11:01

acylam