确定数据框列的数据类型 [英] Determine the data types of a 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
和中间孩子 mode
的更多信息,请参阅此优秀的 SO 线程:对R中事物类型的全面调查.'mode'和'class'和'typeof'不足.
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屋!