专题一:玩转字符串
1.检测字符串长度
代码语言:javascript复制x <- "The birch canoe slid on the smooth planks."
x
str_length(x)#检测字符串内的字符数,空格也算
length(x)#向量里面元素的个数
2.字符串拆分
代码语言:javascript复制str_split(x," ")#以空格为分隔符号将字符串拆分开
x2 = str_split(x," ")[[1]];x2
y = c("jimmy 150","nicker 140","tony 152")
str_split(y," ")
str_split(y," ",simplify = T)#把列表简化为矩阵
3.按位置提取字符串
代码语言:javascript复制str_sub(x,5,9)#取5~9位
str_sub(x,c(5,9))#分别从第5位和第9位开始
str_sub(x,c(5),c(5))#从第5位开始到第5位结束,用于取单独字符
4.字符检测
代码语言:javascript复制str_detect(x2,"h")
str_starts(x2,"T")
str_ends(x2,"e")
5.字符串替换
代码语言:javascript复制x2
str_replace(x2,"o","A")#默认只替换第一个
str_replace_all(x2,"o","A")
6.字符删除
代码语言:javascript复制x
str_remove(x," ")#默认只删第一个
str_remove_all(x," ")
#专题二:玩转数据框
代码语言:javascript复制test <- iris[c(1:2,51:52,101:102),]
rownames(test) =NULL # 去掉行名,NULL是“什么都没有”
test
arrange,数据框按照某一列排序
代码语言:javascript复制library(dplyr)
arrange(test, Sepal.Length) #从小到大
arrange(test, desc(Sepal.Length)) #从大到小,没有increase
distinct,数据框按照某一列去重复
代码语言:javascript复制distinct(test,Species,.keep_all = T)#.keep_all意思是把所有列都保存下来
mutate,数据框新增一列
代码语言:javascript复制mutate(test, new = Sepal.Length * Sepal.Width)
连续的步骤
代码语言:javascript复制# 1.多次赋值,产生多个变量
x1 = filter(iris,Sepal.Width>3)
x2 = select(x1, Sepal.Length,Sepal.Width)
x3 = arrange(x2,Sepal.Length)
# 2.管道符号传递,简洁明了
x = iris %>%
filter(Sepal.Width>3) %>%
select(Sepal.Length,Sepal.Width)%>%
arrange(Sepal.Length)
# 3. 嵌套,代码不易读
arrange(select(filter(iris,Sepal.Width>3),
Sepal.Length,Sepal.Width),
Sepal.Length)
专题三:条件和循环
一.条件语句
代码语言:javascript复制if(){ }
*只有if没有else,那么条件是FALSE时就什么都不做
i = -1
if (i<0) print('up')
if (i>0) print('up')
#理解下面代码
if(!require(tidyr)) install.packages('tidyr')
有else
i =1
if (i>0){
print(' ')
} else {
print("-")
}
i = 1
ifelse(i>0," ","-")
x = rnorm(3)
x
ifelse(x>0," ","-")
#ifelse() str_detect(),王炸
samples = c("tumor1","tumor2","tumor3","normal1","normal2","normal3")
k1 = str_detect(samples,"tumor");k1
ifelse(k1,"tumor","normal")
k2 = str_detect(samples,"normal");k2
ifelse(k2,"normal","tumor")
多个条件
i = 0
if (i>0){
print(' ')
} else if (i==0) {
print('0')
} else if (i< 0){
print('-')
}
ifelse(i>0," ",ifelse(i<0,"-","0"))
二、循环语句
代码语言:javascript复制for循环
x <- c(5,6,0,3)
s=0
for (i in x){
s=s i
print(c(i,s))
}
x <- c(5,6,0,3)
s = 0
for (i in 1:length(x)){
s=s x[[i]]
print(c(x[[i]],s))
}
#如何将结果存下来?
s = 0
result = list()
for(i in 1:length(x)){
s=s x[[i]]
result[[i]] = c(x[[i]],s)
}
result
do.call(cbind,result)
创建列表的三种方式
代码语言:javascript复制a=list(a1=1:10,a2=3:8);a
a=list(1:10,3:8);names(a)=c("a1","a2");a
a=list()
a[[1]]=1:10
a[[2]]=3:8
names(a)=c("a1","a2");a
专题四:表达矩阵画箱线图
代码语言:javascript复制# 表达矩阵
set.seed(10086)
exp = matrix(rnorm(18),ncol = 6)#创建一个18个数字,6列的矩阵
exp = round(exp,2)#保留小数点后两位
rownames(exp) = paste0("gene",1:3)
colnames(exp) = paste0("test",1:6)
exp[,1:3] = exp[,1:3] 1#让数据不含有负数
exp
library(tidyr)
library(tibble)
library(dplyr)
dat = t(exp) %>% #转置
as.data.frame() %>% #转换为数据框架
rownames_to_column() %>% #把行名变为正式的一列
mutate(group = rep(c("control","treat"),each = 3))#加上一列
pdat = dat%>%
#宽变长函数
pivot_longer(cols = starts_with("gene"),#我要把哪些列合并成一列
names_to = "gene",#原来的列名变成什么
values_to = "count")#原来的数值变成什么
library(ggplot2)
p = ggplot(pdat,aes(gene,count))
geom_boxplot(aes(fill = group))
theme_bw()
p
p facet_wrap(~gene,scales = "free")
专题五:隐式循环
代码语言:javascript复制#1.apply 处理矩阵或数据框
#apply(X, MARGIN, FUN, …)
#其中X是数据框/矩阵名;
#MARGIN为1表示行,为2表示列,FUN是函数
test<- iris[1:6,1:4]
apply(test, 2, mean)
apply(test, 1, sum)
#2.lapply(list, FUN, …)
# 对列表/向量中的每个元素(向量)实施相同的操作
test <- list(x = 36:33,y = 32:35,z = 30:27);test
#返回值是列表,对列表中的每个元素(向量)求均值(试试方差var,分位数quantile)
lapply(test,mean)
lapply(test,fivenum)
#3.sapply 简化结果,返回矩阵或向量
sapply(test,mean)
sapply(test,fivenum)
class(sapply(test,fivenum))
专题六:两个数据框的链接
代码语言:javascript复制test1 <- data.frame(name = c('jimmy','nicker','Damon','Sophie'),
blood_type = c("A","B","O","AB"))
test1
test2 <- data.frame(name = c('Damon','jimmy','nicker','tony'),
group = c("group1","group1","group2","group2"),
vision = c(4.2,4.3,4.9,4.5))
test2
library(dplyr)
inner_join(test1,test2,by="name")
right_join(test1,test2,by="name")
full_join(test1,test2,by="name")
semi_join(test1,test2,by="name")
anti_join(test1,test2,by="name")