我正在尝试编写一个小测试来检查提供的列是否为数字
fun_df <- function(data,x){
if(is.numeric(data$x){
stop("done!")
}
print("did not pass")
}
fun_df(cars, dist)
什么是指定 is.numeric(data$x)
的最佳方式?我尝试了 {{x}}
、!!x
。但我无法通过考试。我特别想指定 fun_df(cars, dist)
而不是 fun_df(cars, "dist")
最佳答案
使用deparse(substitute())
。
fun_df <- function(data,x) {
xx <- deparse(substitute(x))
if(is.numeric(data[[xx]])){
stop("done!")
}
print("did not pass")
}
fun_df(iris, Sepal.Width)
Error in fun_df(iris, Sepal.Width) : done!
关于r - 如何检查函数调用中的列是否为.numeric()?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69806040/