arrange() doesn't recognize column name parameter

后端 未结 2 2004
闹比i
闹比i 2021-01-03 06:02

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

In the example

2条回答
  •  庸人自扰
    2021-01-03 06:51

    An update is necessary to the good answer by @avid_useR because 'rlang::parse_quosure' is deprecated now.

    To give a short answer to the question how to make 'dplyr::arrange' accept a string or variable containing a string for the column name to sort, you can do:

    target_column = rlang::sym('mean_age')
    df %>% group_by(state) %>% arrange(!!target_column)
    

    OR as one-liner (if you only need to use it once):

    df %>% group_by(state) %>% arrange(!!rlang::sym(target_column))
    

提交回复
热议问题