我有一个数据帧.我想检查class每一列.
x1 = rep(1:4, times=5)
x2 = factor(rep(letters[1:4], times=5))
xdat = data.frame(x1, x2)
> class(xdat)
[1] "data.frame"
> class(xdat$x1)
[1] "integer"
> class(xdat$x2)
[1] "factor"
Run Code Online (Sandbox Code Playgroud)
但是,想象一下,我有很多列,因此需要apply()用来帮助我做到这一点.但它不起作用.
apply(xdat, 2, class)
x1 x2
"character" "character"
Run Code Online (Sandbox Code Playgroud)
为什么我不能apply()用来查看每列的数据类型?或者我该怎么办?
谢谢!
你可以用
sapply(xdat, class)
# x1 x2
# "integer" "factor"
Run Code Online (Sandbox Code Playgroud)
使用apply会强制输出到matrix,矩阵只能容纳一个'类'.如果有'character'列,则结果将是单个'character'类.要理解这个检查
str(apply(xdat, 2, I))
#chr [1:20, 1:2] "1" "2" "3" "4" "1" "2" "3" "4" "1" ...
#- attr(*, "dimnames")=List of 2
# ..$ : NULL
# ..$ : chr [1:2] "x1" "x2"
Run Code Online (Sandbox Code Playgroud)
现在,如果我们检查一下
str(lapply(xdat, I))
#List of 2
#$ x1:Class 'AsIs' int [1:20] 1 2 3 4 1 2 3 4 1 2 ...
#$ x2: Factor w/ 4 levels "a","b","c","d": 1 2 3 4 1 2 3 4 1 2 ...
Run Code Online (Sandbox Code Playgroud)