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


当前回答

开始时最好使用?str()。为了探索一些例子,让我们制作一些数据:

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的解决方案非常精简:

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

使用str()可以得到这些信息和额外的好处(比如因子的级别和每个变量的前几个值):

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()略有不同:

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

有关class、typeof和中间子元素mode的更多信息,请参阅这个优秀的SO线程:R中事物类型的全面调查。'mode'和'class'和'typeof'是不够的。

其他回答

简单地传递你的数据帧到下面的函数:

data_types <- function(frame) {
  res <- lapply(frame, class)
  res_frame <- data.frame(unlist(res))
  barplot(table(res_frame), main="Data Types", col="steelblue", ylab="Number of Features")
}

生成数据帧中所有数据类型的图表。对于虹膜数据集,我们得到以下结果:

data_types(iris)

下面是一个函数,它是helpRFunctions包的一部分,它将返回数据帧中所有不同数据类型的列表,以及与该类型相关的特定变量名。

install.package('devtools') # Only needed if you dont have this installed.
library(devtools)
install_github('adam-m-mcelhinney/helpRFunctions')
library(helpRFunctions)
my.data <- data.frame(y=rnorm(5), 
                  x1=c(1:5), 
                  x2=c(TRUE, TRUE, FALSE, FALSE, FALSE),
                  X3=letters[1:5])
t <- list.df.var.types(my.data)
t$factor
t$integer
t$logical
t$numeric

然后,您可以执行类似var(my.data[t$numeric])的操作。

希望这对你有帮助!

我建议

sapply(foo, typeof)

如果你需要数据帧中向量的实际类型。Class()有点不同。

如果你不需要作为一个向量来获取这个信息(也就是说,你不需要它来做其他的编程),只需要使用str(foo)。

在这两种情况下,foo将被替换为数据帧的名称。

如果你以data.frame(而不是matrix)的形式导入csv文件,你也可以使用summary.default

summary.default(mtcars)

     Length Class  Mode   
mpg  32     -none- numeric
cyl  32     -none- numeric
disp 32     -none- numeric
hp   32     -none- numeric
drat 32     -none- numeric
wt   32     -none- numeric
qsec 32     -none- numeric
vs   32     -none- numeric
am   32     -none- numeric
gear 32     -none- numeric
carb 32     -none- numeric

开始时最好使用?str()。为了探索一些例子,让我们制作一些数据:

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的解决方案非常精简:

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

使用str()可以得到这些信息和额外的好处(比如因子的级别和每个变量的前几个值):

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()略有不同:

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

有关class、typeof和中间子元素mode的更多信息,请参阅这个优秀的SO线程:R中事物类型的全面调查。'mode'和'class'和'typeof'是不够的。