一.玩转字符串
代码语言:javascript复制rm(list = ls())
if(!require(stringr))install.packages('stringr')
library(stringr)
x <- "The birch canoe slid on the smooth planks."
x
### 1.检测字符串长度
str_length(x)#包含数字、字母、空格、符号等
length(x)#字符串数量
### 2.字符串拆分
str_split(x," ")#将x按空格拆分
x2 = str_split(x," ")[[1]];x2
y = c("jimmy 150","nicker 140","tony 152")
str_split(y," ")
str_split(y," ",simplify = T)
### 3.按位置提取字符串
str_sub(x,5,9)#提取x中第5到第9个字符
### 4.字符检测
str_detect(x2,"h")#检测x中的每个字符串是否含有“h”
str_starts(x2,"T")#检测x中的每个字符串是否以“T”开头
str_ends(x2,"e")#检测是否以“e”结尾
### 5.字符串替换
x2
str_replace(x2,"o","A")#只替换字符串中第一个
str_replace_all(x2,"o","A")#替换所有
### 6.字符删除
x
str_remove(x," ")#只删除第一个空格
str_remove_all(x," ")#删除所有空格
二.玩转数据框
代码语言:javascript复制# arrange,数据框按照某一列排序
library(dplyr)
arrange(test, Sepal.Length) #从小到大
arrange(test, desc(Sepal.Length)) #从大到小
# distinct,数据框按照某一列去重复
distinct(test,Species,.keep_all = T)#将数据框test中的Species列去重复
# mutate,数据框新增一列
mutate(test, new = Sepal.Length * Sepal.Width)#新增列new,值为两列的乘积
三.管道符号:%>%:表示传递
代码语言:javascript复制x=iris %>%
filter(Sepal.Width>3) %>%
select(Sepal.Length,Sepal.Width) %>%
arrange(Sepal.Length)
四.条件语句
代码语言:javascript复制###1.if(){ }
#### (1)只有if没有else,那么条件是FALSE时就什么都不做
i = -1
if (i<0) print('up')
if (i>0) print('up')
#理解下面代码
if(!require(tidyr)) install.packages('tidyr')
#### (2)有else
i =1
if (i>0){print(' ')}else {print("-")}
i = 1
ifelse(i>0," ","-")
x = rnorm(3)
x
ifelse(x>0," ","-")#如果i>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")
#### (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"))
四、循环语句
1.for循环:以下两种循环输出结果一样
代码语言:javascript复制x <- c(5,6,0,3)
s=0
for (i in x){#第一轮循环i=5 接着i=6
s=s i
print(c(i,s))}
代码语言:javascript复制x <- c(5,6,0,3)
s = 0
for (i in 1:length(x)){#第一轮循环i=1 接着i=2 i=3 i=4
s=s x[[i]]#在向量中使用[[]]表示给向量取子集 x[[1]]=5 x[[2]]=6
print(c(x[[i]],s))}
如何将结果存下来?:将列表简化为矩阵
代码语言:javascript复制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)
2.列表新建的另一种方式(基于循环)
代码语言:javascript复制m=list()
m[[1]]=iris[1:50,]
m[[2]]=iris[51:100,]