R04

2023-03-18 16:45:35 浏览数 (1)

第一部分:字符串

1 检测字符串长度

代码语言:javascript复制
x = "The birch canoe slid on the smooth planks."
str_length(x)
[1] "The birch canoe slid on the smooth planks."
length(x)
[1] 1

2 字符串拆分

代码语言:javascript复制
str_split(x," ")
[[1]]"The"     "birch"   "canoe"   "slid"    "on"      "the"    "smooth"    "planks."

x2 = str_split(x," ")[[1]];x2
[1]"The"     "birch"   "canoe"   "slid"    "on"      "the"    "smooth"  "planks."

3 按位置提取字符串

代码语言:javascript复制
str_sub(x,5,9)
[1]"birch"

str_sub(x,6,9)
[1]"irch"

4 字符检测

代码语言:javascript复制
str_detect(x2,"h")
[1]  TRUE  TRUE FALSE FALSE FALSE  TRUE  TRUE FALSE

str_starts(x2,"T")
[1]  TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE

str_ends(x2,"e")
[1]  TRUE FALSE  TRUE FALSE FALSE  TRUE FALSE FALSE

5 字符替换

代码语言:javascript复制
x2
[1] "The"     "birch"   "canoe"   "slid"    "on"      "the"    "smooth"  "planks."

str_replace(x2,"o","A")
[1] "The"     "birch"   "canAe"   "slid"    "An"      "the"    "smAoth"  "planks."

str_replace_all(x2,"o","A")
[1] "The"     "birch"   "canAe"   "slid"    "An"      "the"    
[7] "smAAth"  "planks."

6 字符删除

代码语言:javascript复制
x
[1]"The birch canoe slid on the smooth planks."

str_remove(x," ")
[1] "Thebirch canoe slid on the smooth planks."

str_remove_all(x," ")
[1] "Thebirchcanoeslidonthesmoothplanks."

arrange()

代码语言:javascript复制
arrange()
test = iris
arrange(test,Sepal.Length)

arrange(test,desc(Sepal.Length))
#按降序

distinct() 去重复

代码语言:javascript复制
distinct(test, Species, .keep)

if不可以放置多个布尔逻辑

ifelse可以安置多个布尔逻辑

对矩阵、数据框循环 apply函数

代码语言:javascript复制
x 
apply(x,1,sum)

挑选出表达矩阵里方差最大的1000个基因

代码语言:javascript复制
load("test2.Rdata")
apply(test,1,var)

sort(apply(test,1,var))

tail(sort(apply(test,1,var)),1000)

列表的隐式循环 lapply,结果输出是列表

代码语言:javascript复制
lapply

直接对lapply的结果进行简化,sapply

代码语言:javascript复制
sapply

inner_join 交集,取交集的函数,和merge类似

left_join, 保留左侧表的全部数据,右侧做补充,补充不上则自动填充NA

right_join

full_join 全连接

一些补充函数

dir

0 人点赞