x <- "The birch canoe slid on the smooth planks."
x
### 1.检测字符串长度
str_length(x)
length(x) #返回字符串的个数
split返回列表,但是列表不能进行计算,对列表进行取子集
### 2.字符串拆分
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) #返回矩阵
注意:simplify=T 搜索
### 3.按位置提取字符串
str_sub(x,5,9)
### 4.字符检测
str_detect(x2,"h")
str_starts(x2,"T")
str_ends(x2,"e")
### 5.字符串替换
x2
str_replace(x2,"o","A") #只替换第一个
str_replace_all(x2,"o","A")
### 6.字符删除
x
str_remove(x," ")
str_remove_all(x," ")
test <- iris[c(1:2,51:52,101:102),]
rownames(test) =NULL # 去掉行名,NULL是“什么都没有”
test
# arrange,数据框按照某一列排序
library(dplyr)
arrange(test, Sepal.Length) #从小到大
arrange(test, desc(Sepal.Length)) #从大到小
# distinct,数据框按照某一列去重复
distinct(test,Species,.keep_all = T)
# mutate,数据框新增一列
mutate(test, new = Sepal.Length * Sepal.Width) #new为新增列的名字
管道符号
练习题
# 1.读取group.csv,从第二列中提取圈出来的信息
library(stringr)
a = read.csv("group.csv")
g = str_split(a$title," ",simplify = T)
g
g[,4]
# 2.如何把上一题结果中的Control和Vemurafenib改成全部小写?搜索一下
tolower(g[,4])
str_to_lower(g[,4])
ifelse:
ifelse()+str_detect(),王炸
#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") #警惕写的代码结果顺序和实际相反
#### (3)多个条件
i = 0
if (i>0){
print('+')
} else if (i==0) {
print('0')
} else if (i< 0){
print('-')
}
ifelse(i>0,"+",ifelse(i<0,"-","0"))
for (i in x) {CODE}
下标循环用两个中括号
identical(x1,x2) #判断两个变量是否一致
library(ggplot2)
a = data.frame(a1 = rnorm(15),
a2 = rnorm(15),
a3 = rnorm(15),
g = rep(c("a","b","c"),each = 5))
a
#下标循环不保存
library(ggplot2)
for (i in 1:3) {
p = ggplot(data = a)+
geom_boxplot(aes_string(y = a1[[i]],
x = g))
print(p)
}
# 元素循环,不方便保存
for (i in colnames(a)[1:3]) {
p = ggplot(data = a)+
geom_boxplot(aes_string(y = i,
x = "g"))
print(p)
}
# 下标循环,可以保存数据
p = list()
for (i in 1:3) {
p[[i]] = ggplot(data = a)+
geom_boxplot(aes_string(y = colnames(a)[[i]],
x = "g"))
print(p[[i]])
}
library(patchwork)
p[[1]]+p[[2]]+p[[3]]
# 表达矩阵
set.seed(10086)
exp = matrix(rnorm(18),ncol = 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() %>% #rownames变成一列
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") #分面
如何取数字中最大10个数
a= norm(100)
tail(sort(a),10)
输入数据是列表,输出数据也是列表
### 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))
>ppt和代码来源于生信技能树
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。