函数与参数
1、形式参数与实际参数
2、写函数的函数

3、默认参数
练习:
选ace
> jimmy <- function(a,b,m = 2){+ (a+b)^m+ }> jimmy(a = 1,b = 2)[1] 9> jimmy(1,2)[1] 9> jimmy(3,6)[1] 81> jimmy(3,6,-2)[1] 0.01234568>> #复习:绘图函数plot()> par(mfrow = c(2,2)) #把画板分成四块,两行两列> #如果报错,把右下角画板拉大一点即可> x = c(2,5,6,2,9);plot(x)> x = seq(2,80,4);plot(x)> x = rnorm(10);plot(x)> x = iris$Sepal.Length;plot(x)>> #思考:plot画iris的前四列?> plot(iris[,1],col = iris[,5])> plot(iris[,2],col = iris[,5])> plot(iris[,3],col = iris[,5])> plot(iris[,4],col = iris[,5])>> #当一个代码需要复制粘贴三次,就应该写成函数或使用循环>> jimmy <- function(i){+ plot(iris[,i],col=iris[,5])+ }>> jimmy(1)> jimmy(2)> jimmy(3)> jimmy(4)>练习> # 2、写一个函数,参数是一个数值型向量,输出结果是该向量的平均值加2倍的标准差,> #并写出用户使用该函数的代码 。> joanna <- function(a){+ mean(a)+2*sd(a) #2可以直接些,不需要用m=2+ }> joanna(rnorm(2))[1] 2.978756>
R包
1、R包:大致为多个函数的大包存放,含函数、数据、帮助文件、描述文件
2、安装目的:特定分析功能,需要用相应的包实现。eg.作图包ggplot2,差异分析limma
3、来源:

安装后需加载【一次安装,每次打开新的session都要加载】
library() library(string)【检查是否安装成功的标准,没有error】
require()
快速下载的方式

利用代码【每次新启动时,需要重新加载代码】


安装包——加载包——使用包里的函数
# R包安装options("repos"=c(CRAN="http://mirrors.tuna.tsinghua.edu.cn/CRAN/"))options(BioC_mirror="http://mirrors.ustc.edu.cn/bioc/")install.packages("tidyr")install.packages('BiocManager')BiocManager::install("ggplot2")install.packages('devtools')devtools::install_github("jmzeng1314/idmap1") #括号里写作者用户名加包名# 清华镜像# http://mirrors.tuna.tsinghua.edu.cn/CRAN/# http://mirrors.tuna.tsinghua.edu.cn/bioconductor/# 中科大镜像# http://mirrors.ustc.edu.cn/CRAN/# http://mirrors.ustc.edu.cn/bioc/
安装出错时的问题

# 分情况讨论if(!require(stringr))install.packages("stringr")#分情况安装,#如有就不安装;如没有会安装。也就是一定能安装上这个包library(stringr)
帮助文档
①?sd
②直接搜
③Vignettes
④ls()
ls(“package:stringr”)







解决问题
