r - 如何检查函数调用中的列 is.numeric() 是否?
问题描述
我正在尝试编写一个小测试来检查提供的列是否为数字
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!
推荐阅读
- excel - Excel 宏在使用快捷键打开的 .csv 文件上停止
- data-visualization - 如何显示从谷歌搜索控制台到数据工作室的前 5 个登陆页面?
- python - 我想结合使用 map()、lambda 和 enumerate
- angular - 如何在不注入的情况下使用或替换 Angular 的 HttpClient
- python - 将更多 Celery 任务生成到链中的组中
- c - 为什么 Cygwin gdb 在调试带有线程的程序时会出现问题?
- python - 在函数内部定义之前使用“全局”关键字会发生什么?
- php - 我应该将完整条件传递给要根据它过滤结果的 Select SQL 查询,还是只获取数据并在 php 中过滤它们?
- ios - 从剪贴板复制的快速字符串中的反斜杠撇号转义字符的问题
- javascript - Webpack:SCSS 到 CSS 文件