确定R数据框的列的数据类型? [英] Determine the data types of an R data frame's columns?
问题描述
我正在使用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"
有关 class
, typeof
和中间小孩模式
,看到这个优秀的SO线程:对模式和类和类型中的事物类型的综合调查不足。
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.
这篇关于确定R数据框的列的数据类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!