R 确定数据帧的数据类型';s列

R 确定数据帧的数据类型';s列,r,dataframe,types,R,Dataframe,Types,我使用的是R,并使用read.csv()将数据加载到数据框中。如何确定数据框中每列的数据类型 sapply(yourdataframe, class) 其中yourdataframe是您正在使用的数据帧的名称您最好从使用开始。为了探索一些示例,让我们制作一些数据: set.seed(3221) # this makes the example exactly reproducible my.data <- data.frame(y=rnorm(5),

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

sapply(yourdataframe, class)

其中yourdataframe是您正在使用的数据帧的名称

您最好从使用开始。为了探索一些示例,让我们制作一些数据:

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])
使用
str()

@加文·辛普森(Gavin Simpson)的方法也得到了简化,但提供的信息与
class()
略有不同:

sapply(my.data, typeof)
       y        x1        x2        X3 
"double" "integer" "logical" "integer"
有关
类型
,以及中间子级
模式的更多信息,请参阅此优秀的SO线程:

我建议

sapply(foo, typeof)
如果需要数据帧中向量的实际类型
class()
有点不同

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


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

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

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
install.package('devtools')#只有在未安装此软件包时才需要。
图书馆(devtools)
安装github('adam-m-mcelhinney/helpRFunctions')
库(帮助功能)

my.data只需将数据帧传递到以下函数:

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.frame

dta <- data.frame(a = c(1,2,3), 
                  b = c(4,5,6), 
                  c = c(TRUE, FALSE, TRUE))
如果你有很多专栏,想快速浏览一下,这就非常方便了


值得称赞的是:此解决方案的灵感来自

如果将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

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

library(purrr)
map(df,class)

对于小数据帧:

library(tidyverse)

as_tibble(mtcars)
glimpse(mtcars)
map(mtcars, class)
library(hablar)

mtcars %>% 
  convert(chr(mpg, am),
          int(carb))
提供数据类型的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
为您提供数据类型的结构化视图:

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, ...
$mpg
[1] "numeric"

$cyl
[1] "numeric"

$disp
[1] "numeric"

$hp
[1] "numeric"
提供数据类型的列表:

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, ...
$mpg
[1] "numeric"

$cyl
[1] "numeric"

$disp
[1] "numeric"

$hp
[1] "numeric"
更改列的数据类型:

library(tidyverse)

as_tibble(mtcars)
glimpse(mtcars)
map(mtcars, class)
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
#tible:32 x 11
mpg气缸显示hp drat wt qsec与am齿轮carb
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

在使用R几个月后,我发现
str(dataframe)
是一种快速确定列类型的方法。其他方法需要更多的击键,并且不会显示太多的信息,但是如果列数据类型是其他函数的输入,它们会很有帮助work@DomJo,为什么要使用
apply()
?这是矩阵。数据框是一种(特殊类型的)列表。因为
sappy(foo,typeof)
为日期对象返回“整数”,所以我使用
sappy(foo,class)
。但是,这可以返回一个列表。因此,最后我使用
names(foo)[sappy(sappy(foo,class),function(x){“Date”%in%x})]
来识别
foo
中属于类“Date”的所有列。值得注意的是,在引擎盖下这是
lappy(您的_数据,class)
,需要进行一些额外的格式化处理(…,class))
或交互方式(例如,
str(…)
)或两者兼而有之?通常以编程方式进行更具可扩展性,然后您可以任意
筛选(…)
整数、字符、因子等的列表。或者您可以使用
grep/grepl
名称(…)推断列类型
如果它们遵循任何命名conventions@smci:我在最初的问题中没有要求“以编程方式”。我不知道你为什么要改变我问题的全部性质。好的,它被回滚了。它没有改变全部性质,而是从两个方向之一进行了澄清。使用
str(…)的交互式方法
不可扩展,并且在