[R] Find classes for each column of a data.frame
Barry Rowlingson
b.rowlingson at lancaster.ac.uk
Thu Aug 26 18:16:38 CEST 2010
On Thu, Aug 26, 2010 at 4:44 PM, Marc Schwartz <marc_schwartz at me.com> wrote:
>> sapply(iris, class)
> Sepal.Length Sepal.Width Petal.Length Petal.Width Species
> "numeric" "numeric" "numeric" "numeric" "factor"
Note that comparing the result of class(foo) is a bad way of telling
if something is of a particular class, because of inheritance -
class(foo) can be a vector if the object belongs to more than one
class.
Always test using is.whatever(foo), which returns TRUE if foo is a whatever:
> f=factor(letters)
> class(f)
[1] "factor"
> class(f)=c("factor","alphabet")
> f
[1] a b c d e f g h i j k l m n o p q r s t u v w x y z
Levels: a b c d e f g h i j k l m n o p q r s t u v w x y z
> is.factor(f)
[1] TRUE
but now:
> class(f)=="factor"
[1] TRUE FALSE
Barry
More information about the R-help
mailing list