Lecture1:R基础(一) 张伟平 Thursday 27th August,2009 Previous Next First Last Back Forward
Lecture 1: R Ä:() ܲ Thursday 27th August, 2009 Previous Next First Last Back Forward a
Contents 1 Introduction y 1.1 R website..................。...。 1.2 Differences between R and S............. 2 1.3 Start with R.....·..·.。.。·. 5 2 Data with R 12 2.1 Objects······ 12 2.2 Reading data in a file 18 2.3 Saving data..........·..·..· 22 2.4 generating data................. 23 2.5 Manipulating objects ........... 28 2.5.1 Creating objects·..·.· 28 2.5.2 Operators········· 36 2.5.3 Accessing the values of an object:the indexing system 40 2.5.4 Accessing the values of an object with names 43 2.5.5 Arithmetics and simple functions··········· 45 2.5.6 Matrix Computation.,···.············ 50 Previous Next First Last Back Forward 1
Contents 1 Introduction 1 1.1 R website . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 1.2 Differences between R and S . . . . . . . . . . . . . . . . . . 2 1.3 Start with R . . . . . . . . . . . . . . . . . . . . . . . . . . 5 2 Data with R 12 2.1 Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 2.2 Reading data in a file . . . . . . . . . . . . . . . . . . . . . 18 2.3 Saving data . . . . . . . . . . . . . . . . . . . . . . . . . . . 22 2.4 generating data . . . . . . . . . . . . . . . . . . . . . . . . . 23 2.5 Manipulating objects . . . . . . . . . . . . . . . . . . . . . . 28 2.5.1 Creating objects . . . . . . . . . . . . . . . . . . . . 28 2.5.2 Operators . . . . . . . . . . . . . . . . . . . . . . . . 36 2.5.3 Accessing the values of an object: the indexing system 40 2.5.4 Accessing the values of an object with names . . . . 43 2.5.5 Arithmetics and simple functions . . . . . . . . . . . 45 2.5.6 Matrix Computation . . . . . . . . . . . . . . . . . . 50 Previous Next First Last Back Forward 1
Chapter 1 Introduction R是一个免费开源的用于统计计算和作图的语言和软件环境.支持的操作系 统包括各种UNIX平台,Vindows和MacOS等.R提供了广泛的统计工具(线 性和非线性建模,经典的统计检验,时间序列,生存分析,分类,聚类..等等) 和灵活高质量的图形工具.R的功能可以通过添加package来扩充. 1.1 R website R的官方网站为R Project Website.该网站包含R在各种操作系统下的安 装文件,R的帮助文档包括一些免费的书籍等等.目前R软件的最新版本 为2.9.1.R软件在Windows下的安装是非常简单的.在其它的操作系统下的 安装也很容易,请参考RFAQ:How can R be installed. 着重于R在特殊的应用领域以及统计模型等方面的项目包括 Bioconductor:Bioinformatics with R Previous Next First Last Back Forward
Chapter 1 Introduction R ´¤m ^uÚOOÚãóÚ^¸. |±öX Ú)«UNIX², Windows ÚMacOS . R Jø 2ÚOóä( 5Ú5ï, ²;ÚOu, mS, )©Û, ©a, àa... ) Ú(¹pþã/óä. R õU±ÏLV\ package 5*¿. 1.1 R website R (ÕR Project Website. TÕ¹ R 3«öXÚeS C©, R Ï©) ¤Ö7. 8c R ^# 2.9.1. R ^3Windows eSC´~{ü. 3Ù§öXÚe SCéN´, ëR FAQ: How can R be installed. XuR 3AÏA^+±9ÚO.¡8) • Bioconductor: Bioinformatics with R Previous Next First Last Back Forward 1
1.INTRODUCTION Rgeo:Spatial Statistics with R .gR:gRaphical models in R Robust:Robust Statistics with R Rmetrics:Financial Market Analysis with R 1.2 Differences between R and S l.R由Ross Thaka和Robert Gentleman共同创立,可以视为是由AT&T 贝尔实验室所创的S语言的另外一种实施.S语言现在主要内含 在Insightful公司的S-PLUS软件中. 2.R和S在设计理念上存在有若许多不同.我们可以视S为一种目前有着 三种引繁或者内核的语言:“old S engine”(S version;S-Plus3.x and4.),“new S engine”(S version4;S-Plus5.r and above).以 及R.因此R和S的差异主要是“engines”. 以下S指S引擎,R指R引繁 Previous Next First Last Back Forward
• Rgeo: Spatial Statistics with R • gR: gRaphical models in R • Robust: Robust Statistics with R • Rmetrics: Financial Market Analysis with R 1.2 Differences between R and S 1. R dRoss Ihaka ÚRobert Gentleman ÓMá, ±À´dAT&T ¢¿¤MS ó, «¢. S óy3ÌS¹ 3Insightful úiS-PLUS ^¥. 2. R Ú S 3Ongþ3kXNõØÓ. ·±ÀS «8ckX n«Ú½öSØó: /old S engine0(S version 3; S-Plus 3.x and 4.x), /new S engine0(S version 4; S-Plus 5.x and above), ± 9R. ÏdR ÚS ÉÌ´/engines0. ±eS S Ú, R R Ú. 1. INTRODUCTION Previous Next First Last Back Forward 2
1.INTRODUCTION 1.词法作用域(lexical scope):考虑如下函数 TCode cube <-function(n){ sq <function()n *n n sq() 在S下,sq()不知道变量n,除非它全局指定. TExample S>cube(2) Error in sq():Object "n"not found Dumped S>n<-3 S>cube(2) [1]18 Previous Next First Last Back Forward 3
1. c{^(lexical scope): ÄXe¼ê ↑Code cube <- function(n) { sq <- function() n * n n * sq() } ↓Code 3S e, sq() ØCþn, ا۽. ↑Example S> cube(2) Error in sq(): Object "n" not found Dumped S> n <- 3 S> cube(2) [1] 18 1. INTRODUCTION Previous Next First Last Back Forward 3