大多数R对象都是基于S3类(来源于第三代S语言),例如直方图函数hist()输出是一个包含多个组件的列表,它还有一个属性(attribute),用来指定列表的类,即histogram类。

泛型函数

类用在泛型函数中,泛型函数是一个函数族,其中的每个函数都有相似的功能,但是适用于某个特定的类。比如summary(),它是生成摘要的函数族,R会为要处理的类寻找合适的摘要函数,并使用比列表更加合理的方式来展示。因此对于hist()和lm()它会调用不同的摘要函数。(lm是linear model的缩写)
同样的plot()也是这样的。

> a <- hist(Nile)
> summary(a)
         Length Class  Mode     
breaks   11     -none- numeric  
counts   10     -none- numeric  
density  10     -none- numeric  
mids     10     -none- numeric  
xname     1     -none- character
equidist  1     -none- logical  
> plot(a)

> b <- lm(Nile~c(1:100))
> plot(b)
Hit <Return> to see next plot: 
Hit <Return> to see next plot: 
Hit <Return> to see next plot: 
Hit <Return> to see next plot:
> summary(b)

Call:
lm(formula = Nile ~ c(1:100))

Residuals:
    Min      1Q  Median      3Q     Max 
-483.71  -98.17  -23.21  111.40  368.72 

Coefficients:
             Estimate Std. Error t value Pr(>|t|)    
(Intercept) 1056.4224    30.3377  34.822  < 2e-16 ***
c(1:100)      -2.7143     0.5216  -5.204 1.07e-06 ***
---
Signif. codes:  
0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 150.6 on 98 degrees of freedom
Multiple R-squared:  0.2165,    Adjusted R-squared:  0.2085 
F-statistic: 27.08 on 1 and 98 DF,  p-value: 1.072e-06
内容来源于网络如有侵权请私信删除
你还没有登录,请先登录注册
  • 还没有人评论,欢迎说说您的想法!