如何获取数据框中所有列的类? [英] How do I get the classes of all columns in a data frame?

查看:52
本文介绍了如何获取数据框中所有列的类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

找出数据框中每一列属于哪个类的简单方法是什么?

What is an easy way to find out what class each column is in a data frame is?

推荐答案

一种选择是使用 lapplyclass.例如:

One option is to use lapply and class. For example:

> foo <- data.frame(c("a", "b"), c(1, 2))
> names(foo) <- c("SomeFactor", "SomeNumeric")
> lapply(foo, class)
$SomeFactor
[1] "factor"

$SomeNumeric
[1] "numeric"

另一个选项是str:

> str(foo)
'data.frame':   2 obs. of  2 variables:
 $ SomeFactor : Factor w/ 2 levels "a","b": 1 2
 $ SomeNumeric: num  1 2

这篇关于如何获取数据框中所有列的类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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