R语言对象的类型:
logical
numeric
complex
character
> mode(xxx)
[1] "numeric"
> mode(z)
[1] "complex"
>
> mode(xxx)
[1] "numeric"
> mode(z)
[1] "complex"
> is.numeric(xxx)
[1] TRUE
> is.numeric(z)
[1] FALSE
> is.complex(z)
[1] TRUE
>
> is.logical(c(1)>2)
[1] TRUE
>
> is.numeric(z)
[1] FALSE
> is.complex(z)
[1] TRUE
> is.character(c("AA"))
[1] TRUE
R对象的长度
> length(xxx)
[1] 3
>
R对象类型转换
> x<-c("1","2","3","4")
[1] "1" "2" "3" "4"
> as.numeric(x)->myd
> myd
[1] 1 2 3 4
>
> as.character(myd)
[1] "1" "2" "3" "4"
>
R对象长度
> x<-numeric()
> x[5]<-22
> x
[1] NA NA NA NA 22
> x[4:5]->x
> x
[1] NA 22
> 6->length(x)
> x
[1] NA 22 NA NA NA NA
>
各种属性
> attributes(xxx)
$names
[1] "x1" "x2" "x3"
>
存取某个属性
> attr(xxx,"names")
[1] "x1" "x2" "x3"
> attr(xxx,"names")<-c("n1","n2","n3")
> xxx
n1 n2 n3
11 22 33
>
定义新的属性types
> attr(xxx,"types")<-c("test")
> xxx
n1 n2 n3
11 22 33
attr(,"types")
[1] "test"
>
> attr(xxx,"types")
[1] "test"
>
类的定义
Object Classes
Description
R possesses a simple generic function mechanism which can be used for an object-oriented style of programming. Method dispatch takes place based on the class of the first argument to the generic function.
Usage
class(x) class(x) <- value unclass(x) inherits(x, what, which = FALSE) oldClass(x) oldClass(x) <- value
Arguments
x |
a R object |
what, value |
a character vector naming classes. |
which |
logical affecting return value: see ‘Details’. |
Details
Here, we describe the so called “S3” classes (and methods). For “S4” classes (and methods), see ‘Formal classes’ below.
Many R objects have a class
attribute, a character vector giving the names of the classes from which the object inherits. If the object does not have a class attribute, it has an implicit class, "matrix"
, "array"
or the result of mode(x)
(except that integer vectors have implicit class "integer"
). (Functions oldClass
and oldClass<-
get and set the attribute, which can also be done directly.)
When a generic function fun
is applied to an object with class attribute c("first", "second")
, the system searches for a function called fun.first
and, if it finds it, applies it to the object. If no such function is found, a function called fun.second
is tried. If no class name produces a suitable function, the function fun.default
is used (if it exists). If there is no class attribute, the implicit class is tried, then the default method.
The function class
prints the vector of names of classes an object inherits from. Correspondingly, class<-
sets the classes an object inherits from. Assigning a zero-length vector or NULL
removes the class attribute.
unclass
returns (a copy of) its argument with its class attribute removed. (It is not allowed for objects which cannot be copied, namely environments and external pointers.)
inherits
indicates whether its first argument inherits from any of the classes specified in the what
argument. If which
is TRUE
then an integer vector of the same length as what
is returned. Each element indicates the position in the class(x)
matched by the element of what
; zero indicates no match. If which
is FALSE
then TRUE
is returned by inherits
if any of the names in what
match with any class
.
All but inherits
are primitive functions.
Formal classes
An additional mechanism of formal classes, nicknamed “S4”, is available in packages methods which is attached by default. For objects which have a formal class, its name is returned by class
as a character vector of length one and method dispatch can happen on several arguments, instead of only the first. However, S3 method selection attempts to treat objects from an S4 class as if they had the appropriate S3 class attribute, as does inherits
. Therefore, S3 methods can be defined for S4 classes. See the ‘Classes’ and ‘Methods’ help pages for details.
The replacement version of the function sets the class to the value provided. For classes that have a formal definition, directly replacing the class this way is strongly deprecated. The expression as(object, value)
is the way to coerce an object to a particular class.
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.
Note
Functions oldClass
and oldClass<-
behave in the same way as functions of those names in S-PLUS 5/6, but in R UseMethod
dispatches on the class as returned by class
(with some interpolated classes: see the link) rather than oldClass
. However, group generics dispatch on the oldClass
for efficiency, and internal generics only dispatch on objects for which is.object
is true.
Examples
x <- 10 class(x) # "numeric" oldClass(x) # NULL inherits(x, "a") #FALSE class(x) <- c("a", "b") inherits(x,"a") #TRUE inherits(x, "a", TRUE) # 1 inherits(x, c("a", "b", "c"), TRUE) # 1 2 0