数据内容整理自生信技能树课程
1. 玩转字符串
stringr包
字符串长度 | str_length() |
---|---|
字符串拆分 | str_split() |
按位置提取字符 | str_sub() |
字符检测 | str_detect() |
字符替换 | str_replace() / str_replace_all() |
字符删除 | str_remove() / str_remove_all() |
注意:
str_length() 包含空格
区分字符和字符串
2. 玩转数据框
排序 | arrange() |
---|---|
去重复 | distinct() |
数据框新增列 | mutate() |
test <- iris[c(1:2,51:52,101:102),]
rownames(test) =NULL # 去掉行名,NULL是“什么都没有”
test
1.arrange()
# arrange,数据框按照某一列排序,列于列的对应关系不变(同一行不变)
library(dplyr)
arrange(test, Sepal.Length) #从小到大
arrange(test, desc(Sepal.Length)) #从大到小
2.distinct()
# distinct,数据框按照某一列去重复
distinct(test,Species,.keep_all = T) #保留其它
distinct(test,Species)
3.mutate()
# mutate,数据框新增一列
mutate(test, new = Sepal.Length * Sepal.Width) #名字为new,内容为等号右侧
test
连续的步骤
%>% 管道符号,向后传递
iris %>% # %>% 管道符号,向后传递
select(-5) %>%
as.matrix() %>%
head(50) %>%
pheatmap::pheatmap()
3. 条件和循环
一)条件语句
if(1个逻辑值){ }
- 只有if没有else,那么条件是FALSE时就什么都不做
- 有else
#理解
i = 1
ifelse(i>0,"+","-") # 条件判断,yes,no
- 多个条件
ifelse(i>0,"+",ifelse(i<0,"-","0"))
二)for循环
for(i in x){ }
对x里的每个