If I want to check whether a variable inherits from some class, I can either use is or inherits.
class(letters)
## [1] "character"
is(letters, "character")
## [1] TRUE
inherits(letters, "character")
## [1] TRUE
Is there a preference for which one I should use, and do they ever return different values?
Short version:
Use
inherits, but be careful with numbers and S4 classes.Longer version:
From the See Also section of the
ishelp page:From the Formal Classes section of the
inheritshelp page:So they mostly return the same thing, but
inheritsis faster, so it should be the default choice in most cases. (As mentioned by Konrad,isalso requires that themethodspackage is loaded, which may make it unsuitable for performance sensitive uses ofRscript.)The values can differ if you are using S4 classes with conditional inheritance, but this is not recommended (see "Method Selection and Dispatch: Details" section), which means that it is hopefully rare.
The most obvious place where the two functions differ is when checking if integers are numeric.