R:确定数据框中数值变量的优雅方法
这是我用来在数据框中查找数字变量的代码:
Data <- iris
numericvars <- NULL
for (Var in names(Data)) {
if(class(Data[,Var]) == 'integer' | class(Data[,Var]) == 'numeric') {
numericvars <- c(numericvars,Var)
}
}
numericvars
有没有一种不那么循环的方法来做到这一点?
Here's the code I use to find numeric variables in a data frame:
Data <- iris
numericvars <- NULL
for (Var in names(Data)) {
if(class(Data[,Var]) == 'integer' | class(Data[,Var]) == 'numeric') {
numericvars <- c(numericvars,Var)
}
}
numericvars
Is there a less loopy way to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这是一个非常简单的单行代码
sapply
:This is a pretty simple one-liner with
sapply
:这有点严格:
This is a little tighter:
使用
sapply()
或lapply()
在这里似乎合乎逻辑:这给出了:
值得注意的是,在您的循环中,您正在增长
numericvars
循环每次迭代的向量;在 R 中,这是一个很大的禁忌!它强制 R 每次都复制并扩展向量。事先分配足够的存储并填充对象;在这里,这意味着创建numericvars
,然后在循环中做
更多的工作,但效率更高,尽管只有当迭代次数变大时您才会看到它。
The use of
sapply()
orlapply()
seems logical here:which gives:
Worth noting that in your loop, you are growing the
numericvars
vector at each iteration of the loop; in R, that is a big no-no! It forces R to copy and expand the vector each time. Allocate sufficient storage before hand and fill in the object; here that would mean creatingnumericvars
asthen in the loop doing
A little bit more work, but far more efficient, though you'll only see it when the number of iterations becomes larger.
plyr 中还有
colwise()
、numcolwise()
和catcolwise()
。colwise()
将对向量进行操作的函数转换为对数据帧按列进行操作的函数。numcolwise
和catcolwise
分别提供仅对数字变量和离散变量进行操作的版本。There's also
colwise()
,numcolwise()
andcatcolwise()
in plyr.colwise()
turns a function that operates on a vector into a function that works column-wise on a dataframe.numcolwise
andcatcolwise
provide versions that operate only on numeric and discrete variables respectively.