内容

作者:Sonali Arora (sarora@fredhutch.org)
日期:2015年7月20日至24日

本课程中的材料需要R版本3.2.1和Biocumon V9.2

0.1R

0.2基础r中的有用功能

0.3在R中寻求帮助

0.4r的数据类型

0.5R编程概念

意思是(1:10)
# # 5.5 [1]
rnorm(1:10)
## [9] 0.33290892 -1.03732032 0.00358368 -0.45695476 1.00505058 0.67227284 -0.90495556 -1.45142584 ## [9] 0.22424728 -0.20679577
总结(rnorm (1:10))
##最小1区。中位数3区。最大值。## -0.9524 -0.4189 0.4298 0.3124 0.6072 2.2260
data(iris) #找到花瓣所在的行。宽度正好是0.2鸢尾[鸢尾$花瓣. width ==0.2,]
# #萼片。花萼长度。宽度花瓣。花瓣长度。宽度物种# # 1 5.1 3.5 1.4 0.2 setosa # # 2 4.9 3.0 1.4 0.2 setosa # # 3 4.7 3.2 1.3 0.2 setosa # # 4 4.6 3.1 1.5 0.2 setosa # # 5 5.0 3.6 1.4 0.2 setosa # # 8 5.0 3.4 1.5 0.2 setosa # # 9 4.4 2.9 1.4 0.2 setosa # # 11 5.4 3.7 1.5 0.2 setosa # # 12 4.8 3.4 1.6 0.2 setosa # # 15 5.8 4.0 1.2 0.2 setosa # # 21 5.4 3.4 1.7 0.2 setosa # # 23 4.6 3.6 1.0 0.2setosa # # 25 4.8 3.4 1.9 0.2 setosa # # 26 5.0 3.0 1.6 0.2 setosa # # 28日5.2 - 3.5 1.5 0.2 setosa # # 29日5.2 - 3.4 1.4 - 0.2 setosa # # 30 setosa # # 31日4.7 3.2 1.6 0.2 4.8 3.1 1.6 0.2 setosa # # 34 5.5 4.2 1.4 0.2 setosa # # 35 4.9 3.1 1.5 0.2 setosa # # 36 5.0 3.2 1.2 0.2 setosa 37 # # 5.5 3.5 1.3 0.2 setosa # # 39 4.4 3.0 1.3 0.2 setosa 40 # # 5.1 3.4 1.5 0.2setosa 43 # # 4.4 3.2 1.3 0.2 setosa # # 47 5.1 3.8 1.6 0.2 setosa # # 48 4.6 3.2 1.4 0.2 setosa # # 49 5.3 3.7 1.5 0.2 setosa # # 50 5.0 3.3 1.4 0.2 setosa
#查找sepal.length小于4.5虹膜[虹膜$ sepal.length <4.5,]
## Sepal.Length Sepal.Width Petal.Length Petal.Width Species ## 9 4.4 2.9 1.4 0.2 Setosa ## 14 4.3 3.0 1.1 0.1 Setosa ## 39 4.4 3.0 1.3 0.2 Setosa ## 43 4.4 3.2 1.3 0.2 Setosa
#查找所有属于setosa的行setosa_iris = iris[iris$Species=="setosa",] dim(setosa_iris)
## [1] 50 5
头(setosa_iris)
# #萼片。花萼长度。宽度花瓣。花瓣长度。setosa ## 5 5 3.6 1.4 0.2 setosa ## 6 5.4 3.9 1.7 0.4 setosa
#删除包含字符的列,例如:Species iris <- iris[,!(名称(鸢尾)%在%“品种”)]dim(鸢尾)
## [1] 150
lapply(iris, mean):求前4列数字的平均值
## $ Sepal.Length ## [1] 5.843333 ### $ Sepal.Width ## [1] 3.057333 ### $ Petal.Length ## [1] 3.758 ## ## $ Petal.Width ##[1] 1.199333
#简化结果sapply(iris, mean)
## Sepal.Length Sepal.Width Petal.Length Petal.Width ## 5.843333 3.057333 3.057333 3.758000 1.199333
#找出每一行的平均值。apply(iris, 1, mean) #更简单:rowMeans(iris)
# # [1] 2.550 2.375 2.350 2.350 2.550 2.850 2.425 2.525 2.225 2.400 2.700 2.500 2.325 2.125 - 2.800 # # [16] 3.000 2.750 2.575 2.875 2.675 2.675 2.675 2.350 2.650 2.575 2.450 2.600 2.600 2.550 - 2.425 # # [31] 2.425 2.675 2.725 2.825 2.425 2.400 2.625 2.500 2.225 2.550 2.525 2.100 2.275 2.675 - 2.800 # # [46] 2.375 2.675 2.350 2.675 2.475 4.075 3.900 4.1003.275 3.850 3.575 3.975 - 2.900 3.850 - 3.300 # # [61] 2.875 3.650 3.300 3.775 3.350 3.900 3.650 3.400 3.600 3.275 3.925 3.550 3.800 3.700 - 3.725 # # [76] 3.850 3.950 4.100 3.725 3.200 3.200 3.150 3.400 3.850 3.600 3.875 4.000 3.575 3.500 - 3.325 # # [91] 3.425 3.775 3.400 2.900 3.450 3.525 3.525 3.675 2.925 3.475 4.525 3.875 4.525 4.150 4.375 4.825 # # [106]3.400 4.575 4.200 4.850 4.200 4.075 4.350 3.800 4.025 4.300 4.200 5.100 4.875 3.675 ## [121] 4.525 3.825 4.800 3.925 4.450 4.550 3.900 3.950 4.225 4.400 4.550 5.025 4.250 3.925 3.925 ## [136] 4.775 4.425 4.200 3.900 4.375 4.450 4.350 3.875 4.550 4.550 4.300 3.925 4.175 4.325 3.950

0.6R作为统计计算环境

#定义向量x < -  rnorm(1000)#矢量化计算y < -  x + rnorm(1000,sd = .8)#对象构造df < -  data.frame(x = x,y = y)#线性模型适合< -  lm(y〜x,df)

0.7在r中可视化数据

par(mfrow=c(1,2)) plot(y ~ x, df, cex.lab=2) abline(fit, col="red", lwd=2) library(ggplot2) ggplot(df, aes(x, y)) + geom_point() + stat_smooth(method="lm")

0.8sessionInfo ()

sessionInfo ()
## R版本3.2.1(2015-06-18)##平台:x86_64-unknown-linux-gnu(64位)##运行于:Ubuntu 14.04.2 LTS ## ## locale: ## [1] LC_CTYPE=en_US。utf - 8 LC_NUMERIC = C而= en_US。UTF-8 ## [4] LC_COLLATE=C LC_MONETARY=en_US。utf - 8 LC_MESSAGES = en_US。UTF-8 ## [7] LC_PAPER=en_US。UTF-8 LC_NAME=C LC_ADDRESS= c# # [10] LC_TELEPHONE=C LC_MEASUREMENT=en_US。UTF-8 LC_IDENTIFICATION=C ## ## attached base packages: ## [1] stats graphics grDevices utils datasets methods base ## #### [1] Rcpp_0.11.6 codetools_0.2-14 digest_0.6.8 MASS_7.3-43 grid_3.2.1 ## [6] plyr_1.8.3 gtable_0.1.2 formatR_1.2 magrittr_1.5 scales_0.2.5 ## [11] evaluate_0.7 stringi_0.5-5 reshape2_1.4.1 rmarkdown_0.7 labeling_0.3 ## [16] proto_0.3-10 tools_3.2.1 string_1.0.0 munsell_0.4.2 yaml_2.1.13 ## [21] colorspace_1.2-6 htmltools_0.2.6 knitr_1.10.5