Preferred method of checking object's class in R

风流意气都作罢 提交于 2019-12-23 06:47:30

问题


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)

回答1:


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"))




回答2:


For me it'd be:

is.data.frame(df)

Is a clearer option to use in conditions. Also, is the 'less code' option of the three, if that is important for you.



来源:https://stackoverflow.com/questions/17733500/preferred-method-of-checking-objects-class-in-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!