确定数据框列的数据类型

时间:2014-01-14 22:20:24

标签: r dataframe types

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

9 个答案:

答案 0 :(得分:175)

您最好的选择是使用?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"

有关classtypeof和中间孩子mode的详细信息,请参阅此优秀的SO主题:A comprehensive survey of the types of things in R. 'mode' and 'class' and 'typeof' are insufficient

答案 1 :(得分:36)

sapply(yourdataframe, class)

您的数据框是您正在使用的数据框的名称

答案 2 :(得分:16)

我建议

sapply(foo, typeof)

如果您需要数据框中的实际矢量类型。 class()是一个不同的野兽。

如果您不需要将此信息作为矢量获取(即您以后不需要以编程方式执行其他操作),请使用str(foo)

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

答案 3 :(得分:7)

只需将数据框传递给以下函数:

ghci

生成数据框中所有数据类型的图。对于 iris 数据集,我们得到以下内容:

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")
}

enter image description here

答案 4 :(得分:3)

由于没有明确说明,我只想补充一下:

我一直在寻找一种方法来创建表格,其中包含所有数据类型的出现次数

假设我们有一个dta <- data.frame(a = c(1,2,3), b = c(4,5,6), c = c(TRUE, FALSE, TRUE)) ,其中包含两个数字列和一个逻辑列

table(unlist(lapply(dta, class)))
# logical numeric 
#       1       2 

您可以使用

汇总每种数据类型的列数
myCell.myButton.hidden = NO;

如果您有很多专栏并希望快速了解,这非常方便。

表示赞赏:此解决方案的灵感来自the answer of @Cybernetic

答案 5 :(得分:3)

对于小型数据框:

library(tidyverse)

as_tibble(mtcars)

为您提供带有数据类型的df打印

# A tibble: 32 x 11
     mpg   cyl  disp    hp  drat    wt  qsec    vs    am  gear  carb
 * <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
 1  21       6  160    110  3.9   2.62  16.5     0     1     4     4
 2  21       6  160    110  3.9   2.88  17.0     0     1     4     4
 3  22.8     4  108     93  3.85  2.32  18.6     1     1     4     1

对于大型数据框:

glimpse(mtcars)

为您提供数据类型的结构化视图:

Observations: 32
Variables: 11
$ mpg  <dbl> 21.0, 21.0, 22.8, 21.4, 18.7, 18.1, 14.3, 24.4, 22.8, 19.2, 17.8, 16.4, 17....
$ cyl  <dbl> 6, 6, 4, 6, 8, 6, 8, 4, 4, 6, 6, 8, 8, 8, 8, 8, 8, 4, 4, 4, 4, 8, 8, 8, 8, ...
$ disp <dbl> 160.0, 160.0, 108.0, 258.0, 360.0, 225.0, 360.0, 146.7, 140.8, 167.6, 167.6...
$ hp   <dbl> 110, 110, 93, 110, 175, 105, 245, 62, 95, 123, 123, 180, 180, 180, 205, 215...
$ drat <dbl> 3.90, 3.90, 3.85, 3.08, 3.15, 2.76, 3.21, 3.69, 3.92, 3.92, 3.92, 3.07, 3.0...
$ wt   <dbl> 2.620, 2.875, 2.320, 3.215, 3.440, 3.460, 3.570, 3.190, 3.150, 3.440, 3.440...
$ qsec <dbl> 16.46, 17.02, 18.61, 19.44, 17.02, 20.22, 15.84, 20.00, 22.90, 18.30, 18.90...
$ vs   <dbl> 0, 0, 1, 1, 0, 1, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, ...
$ am   <dbl> 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, ...
$ gear <dbl> 4, 4, 4, 3, 3, 3, 3, 4, 4, 4, 4, 3, 3, 3, 3, 3, 3, 4, 4, 4, 3, 3, 3, 3, 3, ...
$ carb <dbl> 4, 4, 1, 1, 2, 1, 4, 2, 2, 4, 4, 3, 3, 3, 4, 4, 4, 1, 2, 1, 1, 2, 2, 4, 2, ...

要获取列数据类型的列表(如上述@Alexandre所述):

map(mtcars, class)

列出数据类型:

$mpg
[1] "numeric"

$cyl
[1] "numeric"

$disp
[1] "numeric"

$hp
[1] "numeric"

要更改列的数据类型:

library(hablar)

mtcars %>% 
  convert(chr(mpg, am),
          int(carb))

mpgam列转换为字符,并将carb列转换为整数:

# A tibble: 32 x 11
   mpg     cyl  disp    hp  drat    wt  qsec    vs am     gear  carb
   <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <chr> <dbl> <int>
 1 21        6  160    110  3.9   2.62  16.5     0 1         4     4
 2 21        6  160    110  3.9   2.88  17.0     0 1         4     4
 3 22.8      4  108     93  3.85  2.32  18.6     1 1         4     1
 4 21.4      6  258    110  3.08  3.22  19.4     1 0         3     1

答案 6 :(得分:2)

这是一个函数,它是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])的操作。

希望这有用!

答案 7 :(得分:1)

另一个选择是使用purrr包的map函数。

library(purrr)
map(df,class)

答案 8 :(得分:0)

如果您将csv文件作为data.frame(而不是矩阵)导入,则也可以使用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