如何从数据表中只选择数字列 [英] how to select only numeric columns from a data table

查看:108
本文介绍了如何从数据表中只选择数字列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么不使用data.table?它与data.frame一起使用。使用数据表有什么办法吗?

Why doesn't this work with data.table? It works with data.frame. Is there any way to do this with a data table?

x <- data.table(v1=1:20,v2=1:20,v3=1:20,v4=letters[1:20])
y <- x[,sapply(x,is.numeric)] 

这会返回

v1    v2    v3    v4 
TRUE  TRUE  TRUE FALSE 


推荐答案

data.table 需要 with = FALSE 来获取列号。

tokeep <- which(sapply(x,is.numeric))
x[ , tokeep, with=FALSE]

这篇关于如何从数据表中只选择数字列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆