2013-07-22 1 views
7

다음 행동에 대한 설명은 무엇입니까?R에서`is.list (x)`와`is (x, 'list') 사이의 다른 행동`

is.list(data.frame()) ## TRUE 
is(data.frame(),'list') ## FALSE 
is(data.frame()) ## "data.frame" "list" "oldClass" "vector" 
extends('data.frame','list') ## TRUE 
inherits(data.frame(),'list') ## FALSE 
+0

재미있는 정보가있는 또 다른 함수는'getClass ('data.frame')' – eddi

답변

8

S3 및 S4 클래스 규칙을 혼합합니다. isextends은 S4 클래스를위한 것이지만, 이것들은 구현 된 방식 때문에 S3와 작동합니다. inherits은 S3 클래스 용으로 작성되었으며 완전한 호환성을 갖춘 S4 개체와 함께 작동하지 않습니다.

inheritsclass(x)의 결과를 두 번째 인수에서 지정한 클래스와 효과적으로 비교합니다. 따라서

> class(data.frame()) 
[1] "data.frame" 

은 어디에서도 실패합니다. 이 ?inherits에서

참고 :

The analogue of ‘inherits’ for formal classes is ‘is’. The two 
functions behave consistently with one exception: S4 classes can 
have conditional inheritance, with an explicit test. In this 
case, ‘is’ will test the condition, but ‘inherits’ ignores all 
conditional superclasses. 

또 다른 혼란이 객체의 클래스와 객체의 구현입니다. 예, 데이터 프레임은 is.list()이 말하듯이 목록이지만, R의 S3 클래스 세계에서는 data.frame()"data.frame"이 아니고 "list"이 아닙니다.

is(data.frame(),'list')에 대해서는 해당 특정 클래스가 아닙니다. "list" 따라서 FALSE입니다. 어떤 is(data.frame())is(data.frame())는이 클래스 "data.frame"합니다 (S4 감지 아닌 S3 의미에서) 연장 클래스를 보여주는 따라서 ?is

Summary of Functions: 

    ‘is’: With two arguments, tests whether ‘object’ can be treated as 
      from ‘class2’. 

      With one argument, returns all the super-classes of this 
      object's class 

에서 설명 않는다. 이것은 또한 S4 월드에서의 extends('data.frame','list') 동작을 설명하며, "data.frame" 클래스 "list" 클래스를 확장합니다.

관련 문제