《R语言编程艺术》笔记1

来源:互联网 发布:scwozer系统优化工具 编辑:程序博客网 时间:2024/05/20 17:23

(美)NormanMatloff. R语言编程艺术[M]. 机械工业出版社, 2013.

R是一种用于统计计算与作图的开源软件。

CRAN:http://cran.r-project.org

统计之都:http://cos.name

谢益辉博客:http://yihui.name

http://supstat.com

http://yanping.me

http://yixuan.cos.name

最好直接在终端窗口输入命令并提交给R,而不是在GUI中用鼠标点击菜单。免费的GUI,RStudio,StatET,ESS,JGR(Java GUI for R)

因为R是一种编程语言而不是各种不相关的命令汇总,你可以把几个命令组合起来使用,每条命令用前一条命令的输出作为输入。(这类似与Linux中的管道将shell命令串联起来)。这种组合R函数的能力带来了巨大的灵活性,功能十分强大。


回归函数lm()

<span style="font-size:14px;">> demo()> help.start()starting httpd help server ... 做完了。如果什么都不发生的话,你应该自己打开‘http://127.0.0.1:17077/doc/html/index.html’> </span>

<span style="font-size:14px;">> rnorm(100)  [1]  1.346912942 -0.017826569 -0.482013340 -1.053436383 -2.003685616  0.890584043  0.906037362  1.300596677 -0.919223237  2.353733095 -1.159966750 [12] -0.740187571 -0.652145416 -1.353839970 -0.175433307 -1.577254178  0.230385427  0.286321232  1.279781695 -1.025928712 -0.182144373 -1.129482437 [23]  0.483395320 -0.215042960 -0.559509278 -0.456930110  1.237689083  0.110235167  0.708113832  0.002078762 -0.062381806  0.121701246 -0.162525907 [34] -0.980977605 -0.811114764 -0.411689224  1.244884108 -1.357196744 -0.761132943 -0.331461000 -0.044654421 -2.371294806  0.018426351 -0.507129190 [45]  0.690862765  1.547942465 -0.768729136 -1.061435511 -0.086871461 -0.575156867  1.362219050 -0.670643147 -0.772809784 -0.156534972  1.449081224 [56]  0.861920134  0.776456885  0.758845832  0.603882635  0.728163205 -0.279116551 -2.306108036  0.685983661 -1.526437675 -0.639039508  2.787383049 [67]  1.563089181  0.737454036  0.799603248  0.016432187 -0.087996456  0.638301814 -0.991444660  0.607951816 -0.001033449  0.759652515 -0.161885524 [78]  1.074027461 -1.303782690  1.342846691  1.733852446 -0.718228737  1.184304867 -1.319368575  0.319264045 -0.779358051 -0.418071647 -0.746274990 [89] -0.113172981 -0.478591210 -0.028767009 -0.659293091 -1.026634878  0.636776811  0.819336248  0.988597719  0.050099458  1.554004368  0.404014216[100] -0.590338030> abs(rnorm(100))  [1] 0.62191467 1.64375015 0.48056638 0.91896523 1.91464252 2.27740992 0.95863959 0.77486889 0.85510930 1.00369737 1.37899682 0.17943638 0.85251269 [14] 1.96785451 2.17499443 0.34224818 0.80473035 0.08116219 0.22511410 0.90156639 1.15286516 0.33200028 0.75380659 0.10606483 1.68419988 0.41152243 [27] 0.09422269 0.45902071 0.61132377 0.02297269 0.53074582 1.14036239 0.34268551 0.75009600 1.79272605 1.54216812 0.43608916 0.29866178 0.09105205 [40] 0.02652634 2.42784402 1.45547211 1.10223013 0.26740638 1.50357799 0.10557373 0.45873227 1.00897552 1.04240684 0.66139289 0.32120036 0.51526216 [53] 0.50509919 1.17668998 0.35425584 0.39815428 1.54472275 0.96387741 0.89810597 0.09494094 0.26031501 0.43586348 1.31702657 0.56812034 0.63343241 [66] 0.87498752 0.70737035 1.01405419 0.06905642 0.43970709 1.54096639 1.02800369 1.41331551 2.60493017 0.28710021 0.26869242 0.88414873 0.31037364 [79] 0.19485321 0.50186262 0.47269794 1.12557437 0.86317132 0.44684379 0.92424007 0.91192641 0.16677278 1.67156473 0.42313807 1.08613623 0.60047816 [92] 0.76238635 0.02911047 0.89464955 2.08492567 0.08674796 0.53056432 0.41029514 0.91528366 1.60412881> k=abs(rnorm(10))> k [1] 0.9035286 0.8565731 1.2700663 0.2948125 0.6081099 0.2938657 0.2933157 0.7265175 0.8037865 0.9196082> mean(k)[1] 0.6970184> source("E:\\R\\new\\test.R")> test [1] -0.5463123  2.0567967 -0.9764734  1.0905468  0.2421499  1.0828190 -1.3338301 -0.8236004 -0.3131810 -1.2480135> test(2)错误: 没有"test"这个函数> test[2][1] 2.056797> </span>

# 注释部分

<span style="font-size:14px;">> pdf("xh.pdf") #set graphical output file> hist(rnorm(100)) #generate 100 N(0,1) variates and plot their histogram> dev.off()  #close the graphical output filenull device           1 > x <-c(1,2,4)> x[1] 1 2 4> q <-c(x,x,8)> q[1] 1 2 4 1 2 4 8> x[6][1] NA> q[6][1] 4> </span>
R向量的元素索引(index)(下标subscript)从1开始而不是0

计算标准差使用sd函数



q()退出R

R定义函数

<span style="font-size:14px;">#counts the number of odd integers in x oddcount <- function(x){ k <-0 #assign 0 to k for(n in x){if(n%%2 ==1) k<-k+1 #%% is the mudulo operator} return(k)}</span>

取余求模运算符%%

在没有显式调用return时,R会返回最后计算的值,不过这个方法要谨慎使用。

形式参数formal argument或formal parameter,实际参数 actual argument

超赋值运算符 superassignment operator

默认函数 g <-function(x,y=2,z=T){...}

布尔类型 TURE & FALSE  可以缩写为T and F

R语言中的数据结构

向量类型是R语言的核心。 

标量,一元向量

字符串  y<-"abc"   mode(y)  有很多字符串操作函数

<span style="font-size:14px;">> > y<-"abc"> mode(y)[1] "character"> u <-paste("abc","de","f") #concatenate the strings> u[1] "abc de f"> v <- strsplit(u," ") #split the string according to blanks> v[[1]][1] "abc" "de"  "f"  </span>

矩阵

<span style="font-size:14px;">> m <-rbind(c(1,4),c(2,2))> m     [,1] [,2][1,]    1    4[2,]    2    2> m[1,2][1] 4> m[2,2][1] 2> m[1,] #row 1[1] 1 4> m[,2] #column 2[1] 4 2> </span>
列表:和R语言的向量类似,R语言中的列表也是值的容器,不过其内容可以属于不同的数据类型(类似于结构体)。

<span style="font-size:14px;">> x<-list(u=2,v="abc")> x$u[1] 2$v[1] "abc"> x$u[1] 2> x$v[1] "abc"> hn<-hist(Nile)</span>
数据框 :一个典型的数据集包汉多种不同类型的数据。R语言中的数据框实际上是列表,只不过列表中每个组件是由前面提到的矩阵数据的一列所构成的。

<span style="font-size:14px;">> d <- data.frame(list(kids=c("jack","jill"),ages=c(12,10)))> d  kids ages1 jack   122 jill   10> d$ages[1] 12 10> </span>
通常数据框是通过读取文件或数据库来创建的。

类:对象是类的实例。泛型函数代表一个函数族,其中每个函数都有相似的功能,但是适用于某个特定的类。

一个常用的泛型函数就是summary()

summary()函数实际上生成摘要的函数族。hist()的输出结果调用summary函数会生成与之相适应的摘要,而对应函数lm()调用summary时也会生成与之相适应的摘要。

plot()是另一个泛型函数。可以对任何一个R对象使用plot函数,R会根据对象的类寻找合适的画图函数。

类也可以用来组织对象。类与泛型函数结合使用,可以开发出灵活的代码,以处理各种不同的但是相关联的任务。


> getwd()#获得当前工作路径[1] "C:/Users/Administrator/Documents"> setwd("E:\R")#设置当前工作路径错误: 由""E:\R"开头的字符串中存在'\R',但没有这种逸出号> setwd("E:\\R")#设置当前工作路径> getwd()#获得当前工作路径[1] "E:/R"
> ?lm
starting httpd help server ... done
> help(lm)
> ?"<"
> ?"for"

 help.search("multivariate normal")
> ??"nultivariate normal"















0 0
原创粉丝点击