条件语句
if条件语句:如果...就...,否则...
if(一个逻辑值){ 一段代码 } else { 一段代码 }
*若只有if没有else,那么条件是FALSE时就什么都不做
i = -1
if (i<0) print('up')
## [1] "up"
if (i>0) print('up')
#无返回值
if(!require(tidyr)) install.packages("tidyr")
(2)有else
i =1
if (i>0){
cat('+') #输出本来的样子+
} else {
print("-") #输出括号里面的内容“-”
}
ifelse 很重要
image.pngx=rnorm(10)
y=ifelse(x>0,"+","-")
y
[1] "+" "-" "-" "-" "-" "-" "-" "+" "+" "-"
(3)多个条件
i = 0
if (i>0){
print('+')
} else if (i==0) {
print('0')
} else if (i< 0){
print('-')
}
[1] "0"
ifelse(i>0,"+",ifelse((i<0),"-","0"))
[1] "0"
也可以学习一下dyplr中的case_when函数
1. For循环
循环中括号建议写两个
顺便看一下next和break
*用x本身作为参数
x <- c(5,6,0,3)
s=0
for (i in x){ #x里面的i数据依次进入循环计算,四次结束
s=s+i
#if(i == 0) next 跳到下一个循环
#if (i == 0) break 跳出整个循环
print(c(which(x==i),i,1/i,s))
}
[1] 1.0 5.0 0.2 5.0
[1] 2.0000000 6.0000000 0.1666667 11.0000000
[1] 3 0 Inf 11
[1] 4.0000000 3.0000000 0.3333333 14.0000000
*用x的角标作为参数
x <- c(5,6,0,3)
s = 0 #数据的角标1:4
for (i in 1:length(x)){
s=s+x[[i]] ## 循环中 中括号建议写两个
#if(i == 3) next
#if (i == 3) break
print(c(i,x[[i]],1/x[[i]],s))
}
[1] 1.0 5.0 0.2 5.0
[1] 2.0000000 6.0000000 0.1666667 11.0000000
[1] 3 0 Inf 11
[1] 4.0000000 3.0000000 0.3333333 14.0000000
如何将结果存下来?
s = 0
result = list()
for(i in 1:length(x)){
s=s+x[[i]]
result[[i]] = c(i,x[[i]],1/i,s)
}
> View(result)
image.png
do.call(cbind,result) #将结果中的元素按列排列成数据框
## [,1] [,2] [,3] [,4]
## [1,] 1 2.0 3.0000000 4.00
## [2,] 5 6.0 0.0000000 3.00
## [3,] 1 0.5 0.3333333 0.25
## [4,] 5 11.0 11.0000000 14.00
练习6-3
注意最后aes()传参的问题
get()函数可以让字符变成变量名
1.使用循环,查看"a",TRUE和3的数据类型
m <- list("a", TRUE, 3)
for (i in 1:3) {
class(m[[i]])
}
#2.生成10个随机数,根据这10个随机数生成一个新向量,>中位数的值对应"A",<中位数的值对应"B"。
m <- rnorm(10)
m1 <- ifelse(m>median(m), "A", "B");m1
## [1] "A" "A" "B" "A" "B" "A" "B" "B" "B" "A"
#3.根据上一练习题中的tmp2生成一个新向量,含有e的值对应"A",不含有e的值对应"B"
tmp = "Bioinformatics is a new subject of genetic data collection,analysis and dissemination to the research community."
library(stringr)
tmp2 = tmp %>%
str_replace(","," ") %>%
str_remove("[.]") %>%
str_split(" ")
tmp2 = tmp2[[1]]
ifelse(str_detect(tmp2, "e"), "A", "B")
## [1] "B" "B" "B" "A" "A" "B" "A" "B" "A" "B" "B" "A" "B" "A" "A" "B"
#2.生成一个随机数(rnorm)组成的10行6列的矩阵,列名为sample1,sample2….sample6,
# 行名为gene1,gene2…gene10,
# 分组为sample1、2、3属于A组,sample4、5、6属于B组。
# 用循环对每个基因画ggplot2箱线图。
set.seed(2020)
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library(tidyr)
library(ggplot2)
library(cowplot)
##
## ********************************************************
## Note: As of version 1.0.0, cowplot does not change the
## default ggplot2 theme anymore. To recover the previous
## behavior, execute:
## theme_set(theme_cowplot())
## ********************************************************
library(patchwork)
##
## Attaching package: 'patchwork'
## The following object is masked from 'package:cowplot':
##
## align_plots
exp = matrix(rnorm(60),nrow = 10)
colnames(exp) <- paste0("sample",1:6)
rownames(exp) <- paste0("gene",1:10)
exp[1:4,1:4]
## sample1 sample2 sample3 sample4
## gene1 0.3769721 -0.8531228 2.17436525 -0.8125047
## gene2 0.3015484 0.9092592 1.09818265 -0.7437022
## gene3 -1.0980232 1.1963730 0.31822032 1.0953451
## gene4 -1.1304059 -0.3715839 -0.07314756 2.4353737
dat = data.frame(t(exp))
dat = mutate(dat,group = rep(c("A","B"),each = 3))%>% mutate(pair = rep(c("AA","BB","cc"),each = 2))
dat2 = gather(dat,key = "gene",value = "expression",-group,-pair)
ggplot(data = dat2)+
geom_boxplot(aes(x = group,y = expression,color = group))+
theme_bw()+
facet_wrap(~gene,nrow = 2)
image
p <- list()
for (i in colnames(dat)[1:10]) {
p[[which(colnames(dat)==i)]] = ggplot(data = dat,
aes(x = group,y = !!dat[,i], color = group))+
geom_boxplot()+
ylab(paste0("Expression of ", i))
}
wrap_plots(p,nrow=2,guides = 'collect')
image
2.while 循环
image.png不会自己停,需要达到你给出的条件才会停止,很少应用
i = 0
while (i < 5){
print(c(i,i^2))
i = i+1
}
## [1] 0 0
## [1] 1 1
## [1] 2 4
## [1] 3 9
## [1] 4 16
3.repeat 语句
注意:必须有break
i=0L
s=0L
repeat{
i = i + 1
s = s + i
print(c(i,s))
if(i==50) break
}
网友评论