[R] Accessing the class of an object with two elements.

Vincent Goulet vincent.goulet at act.ulaval.ca
Thu Feb 22 17:41:10 CET 2007


Le Jeudi 22 Février 2007 05:37, Shubha Vishwanath Karanth a écrit :
> Hi R,
>
> Here's my question about accessing the class of an object.
>
> I have an object "dat" which can take any two of the classes, ("dates"
> "times") or ("chron" "dates" "times). Note that the classes have two
> elements within it. I want to read these classes in such a way that
>
> v=class(dat) # let class(dat)= "dates" "times"
>
> If(class(dat)==v) k=1 else k=0
>
> The problem is I can't read the above class. The error which I get for
> the above if statement is as follows:
>
> Warning message:
>
> the condition has length > 1 and only the first element will be used in:
> if (class(index(intra)) == v) k = 1
>
> How should I proceed with this? Any ideas? I tried with readline to read
> the class and access it in the 'if' statement...But doesn't work :-(

Well, given your code the condition in the 'if' statement will return c(TRUE, 
TRUE), hence the warning. Executing your code piece by piece would tell you 
that.

That said, you probably rather want to use inherit() for such purposes.

HTH

-- 
  Vincent Goulet, Associate Professor
  École d'actuariat
  Université Laval, Québec 
  Vincent.Goulet at act.ulaval.ca   http://vgoulet.act.ulaval.ca



More information about the R-help mailing list