设为首页 加入收藏

TOP

R语言学习笔记(二): 类与泛型函数
2017-10-09 13:50:42 】 浏览:4888
Tags:语言学习 笔记 函数

大多数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
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇ggplot2图形解析 下一篇R语言学习笔记(三):零碎知识点..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目