是否有使用 is.numeric 的矢量化方式? [英] Is there a vectorized way to use is.numeric?
本文介绍了是否有使用 is.numeric 的矢量化方式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在 SO 阅读这个问题,想知道是否有任何办法以矢量化方式使用 is.numeric.关键是,如果您有一种矢量化方法来检查变量是否为数字,那么任何依赖于数字变量的函数都可以进行矢量化.否则无法矢量化.
I was reading this question at SO and was wondering if there is any way to use is.numeric in a vectorized way. The point being, if you have a vectorized way to check if a variable is numeric, then any function what depends on the variable being numeric can be vectorized. Otherwise, it cannot be vectorized.
推荐答案
根据评论:
如果您想使用 as.numeric 测试数据框的列,请使用 apply
if you're looking to test columns of a data.frame with as.numeric, use apply
> dat <- data.frame(v1=1:5,v2=letters[1:5],v3=rnorm(5),v4=c(1,2,'c','d',5))
> sapply(dat,is.numeric)
v1 v2 v3 v4
TRUE FALSE TRUE FALSE
或者,为了多样化,您可以使用 plyr 包中的 colwise
:
Or, for variety, you can use colwise
from the plyr package:
> colwise(is.numeric)(dat)
v1 v2 v3 v4
1 TRUE FALSE TRUE FALSE
这篇关于是否有使用 is.numeric 的矢量化方式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文