Your best bet is to start using ?str()
. To learn some examples, do some data:
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's solution is very optimized:
sapply(my.data, class) y x1 x2 X3 "numeric" "integer" "logical" "factor"
Using str()
, you get this information plus additional positive effects (such as the levels of your factors and the first few values ββof each variable):
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's approach is also optimized, but provides slightly different information than class()
:
sapply(my.data, typeof) y x1 x2 X3 "double" "integer" "logical" "integer"
For more information on class
, typeof
and the average mode
child, see this excellent SO stream: A comprehensive study of the types of things in R. 'mode' and 'class' and 'typeof' are not enough .
gung Jan 14 '14 at 22:55 2014-01-14 22:55
source share