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


当前回答

我建议

sapply(foo, typeof)

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

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

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

其他回答

要获得一个带有类型和类的漂亮Tibble:

  purrr::map2_df(mtcars,names(mtcars), ~ {
    tibble(
      field = .y,
      type = typeof(.x),
      class_1 = class(.x)[1],
      class_2 = class(.x)[2]
    )
    })

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

library(purrr)
map(df,class)

对于小数据帧:

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

将列MPG和am转换为字符,将列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

因为说得不清楚,我补充一下:

我正在寻找一种方法来创建一个表,其中保存所有数据类型的出现次数。

假设我们有一个data.frame,有两个数字列和一个逻辑列

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 

如果您有很多列,并且想要快速浏览,那么这个方法非常方便。

这个解决方案的灵感来自@Cybernetic的答案。

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

希望这对你有帮助!