###1.if(){ }如果(逻辑值,不是逻辑值向量)就{}
i = -1
if (i<0) print('up')
## [1] "up"
if (i>0) print('up')
#理解下面代码
if(!require(tidyr)) install.packages('tidyr')
i =1
if (i>0){
print('+')
} else {
print("-")
}
## [1] "+"
i = 1
ifelse(i>0,"+","-")
## [1] "+"
x = rnorm(3) # 可以是逻辑值或逻辑值向量
x
## [1] -0.7623049 0.9558385 2.0604772
ifelse(x>0,"+","-")
## [1] "-" "+" "+"
library(stringr)
samples = c("tumor1","tumor2","tumor3","normal1","normal2","normal3")
k1 = str_detect(samples,"tumor");k1
## [1] TRUE TRUE TRUE FALSE FALSE FALSE
ifelse(k1,"tumor","normal")
## [1] "tumor" "tumor" "tumor" "normal" "normal" "normal"
k2 = str_detect(samples,"normal");k2
## [1] FALSE FALSE FALSE TRUE TRUE TRUE
ifelse(k2,"normal","tumor")
## [1] "tumor" "tumor" "tumor" "normal" "normal" "normal"
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"
for( i in 1:4){
print(i)
}
## [1] 1
## [1] 2
## [1] 3
## [1] 4
par(mfrow = c(2,2))
for(i in 1:4){
plot(iris[,i],col = iris[,5])
}
pks = c("tidyr","dplyr","stringr")
for(g in pks){
if(!require(g,character.only = T))
install.packages(g,ask = F,update = F)
}
library(dplyr)
# case_when() # 可用于将数据转换为分类因子
df <- data.frame(player = c('AJ', 'Bob', 'Chad', 'Dan', 'Eric', 'Frank'),
position = c('G', 'F', 'F', 'G', 'C', NA),
points = c(12, 15, 19, 22, 32, NA),
assists = c(5, 7, 7, 12, 11, NA))
df
## player position points assists
## 1 AJ G 12 5
## 2 Bob F 15 7
## 3 Chad F 19 7
## 4 Dan G 22 12
## 5 Eric C 32 11
## 6 Frank <NA> NA NA
df %>%
mutate(quality = case_when(points > 20 ~ 'high',
points > 15 ~ 'med',
TRUE ~ 'low' ))
## player position points assists quality
## 1 AJ G 12 5 low
## 2 Bob F 15 7 low
## 3 Chad F 19 7 med
## 4 Dan G 22 12 high
## 5 Eric C 32 11 high
## 6 Frank <NA> NA NA low
引用自生信技能树
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。