Preferred method of checking object's class in R

pjvandehaar picture pjvandehaar · Jul 18, 2013 · Viewed 7.7k times · Source

What is the preferred method of checking an object's class in R?

(1)

is.data.frame(df)

(2)

class(df) == 'data.frame'

(3)

'data.frame' %in% class(df)

Answer

Ben Bolker picture Ben Bolker · Jul 18, 2013

I would say

inherits(df,"data.frame")

or

is(df,"data.frame")

among other things, #2 in your list can fail because (as you suggest in #3) class(df) can have length > 1. (is.data.frame is nice, but not all classes have is. methods: see methods("is"))