问题描述
在R内,我使用 dplyr
,更具体地说是 arrange()
。
arrange
函数无法按预期工作。
Within R, I use dplyr
and more specifically arrange()
.Somehow the arrange
function doesn't work as expected.
在下面的示例中,我先存储了列的名称,然后将此变量作为参数传递给名为 my_function的自定义函数。
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))
}
由于 arrange():
R returns an error when 'my_function' is called due to the input to arrange()
:
当名称该列的直接放入 arrange()
,而不是引用字符串的变量(如上面的示例),它确实接受参数。
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)
如何更好地将列名的参数传递给'my_function',所以 arrange()
How can I pass the parameter for the column name in a better way to 'my_function', so arrange()
will recognize it?
推荐答案
您需要先将字符串参数解析为quosure,然后用<$ c $取消引用c> !! :
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)
如果想提供 target_column
作为未引用的列名,则可以使用 enquo
代替:
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)
注意:
通常, enquo
也适用于字符串参数,但是 arrange
本身不允许这样做,因此以下内容不适用于第二个示例:
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)
下面是一个玩具示例,用来说明我的意思,因为OP的问题不可重现:
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)
}
结果:
> 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()
这篇关于rang()无法识别列名参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!