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?