确定数据框的列的数据类型 [英] Determine the data types of a data frame's columns

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

问题描述

我正在使用R,并已使用 read.csv()将数据加载到数据框中。如何确定数据框中每一列的数据类型?

I'm using R and have loaded data into a dataframe using read.csv(). How do I determine the data type of each column in the data frame?

推荐答案

您最好的选择是使用 ?str() 。要研究一些示例,让我们做一些数据:

Your best bet to start is to use ?str(). To explore some examples, let's make some data:

set.seed(3221)  # this makes the example exactly reproducible
my.data <- data.frame(y=rnorm(5), 
                      x1=c(1:5), 
                      x2=c(TRUE, TRUE, FALSE, FALSE, FALSE),
                      X3=letters[1:5])

@Wilmer E Henao H的解决方案非常简化:

@Wilmer E Henao H's solution is very streamlined:

sapply(my.data, class)
        y        x1        x2        X3 
"numeric" "integer" "logical"  "factor" 

使用 str()可为您提供这些信息以及额外的好处(例如因素水平和每个变量的前几个值):

Using str() gets you that information plus extra goodies (such as the levels of your factors and the first few values of each variable):

str(my.data)
'data.frame':  5 obs. of  4 variables:
$ y : num  1.03 1.599 -0.818 0.872 -2.682
$ x1: int  1 2 3 4 5
$ x2: logi  TRUE TRUE FALSE FALSE FALSE
$ X3: Factor w/ 5 levels "a","b","c","d",..: 1 2 3 4 5

@Gavin Simpson的方法也得到了简化,但提供的信息与 class()略有不同:

@Gavin Simpson's approach is also streamlined, but provides slightly different information than class():

sapply(my.data, typeof)
       y        x1        x2        X3 
"double" "integer" "logical" "integer"

有关的更多信息typeof 和中间子模式 mode ,请参见以下出色的SO线程:对R中模式,类和类型的事物类型进行了全面调查

For more information about class, typeof, and the middle child, mode, see this excellent SO thread: A comprehensive survey of the types of things in R. 'mode' and 'class' and 'typeof' are insufficient.

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

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