#装包
options("repos" = c(CRAN="http://mirrors.tuna.tsinghua.edu.cn/CRAN/"))
if(!require(tidyr))install.packages("tidyr",update = F,ask = F)
if(!require(dplyr))install.packages("dplyr",update = F,ask = F)
if(!require(stringr))install.packages('stringr',update = F,ask = F)
if(!require(tibble))install.packages('tibble',update = F,ask = F)
library(tidyr)
library(dplyr)
library(stringr)
library(tibble)
rm(list = ls())
if(!require(stringr))install.packages('stringr')
library(stringr)
####1玩转字符串
x <- "The birch canoe slid on the smooth planks."
x
## [1] "The birch canoe slid on the smooth planks."
### 1.str_length 检测字符串长度
str_length(x)#字符串长度
## [1] 42
length(x)#注释str_length(x) 是 stringr 包中的函数,用于计算一个字符串中的字符数(包括空格和标点符号)。length(x) 是 R 语言中的基本函数,用于计算一个对象(如向量、列表等)中元素的数量。
## [1] 1
### str_split字符串拆分,一个元素拆成一个列表
str_split(x," ")#按空格拆分,返回1个元素的列表,此时class是 "list"
## [[1]]
## [1] "The" "birch" "canoe" "slid" "on"
## [6] "the" "smooth" "planks."
x2 = str_split(x," ")[[1]];x2#給列表取子集成字符串向量,此时> class(x2)是 "character"
## [1] "The" "birch" "canoe" "slid" "on"
## [6] "the" "smooth" "planks."
y = c("jimmy 150","nicker 140","tony 152")
str_split(y," ")#此时是3个list,拆成列表
## [[1]]
## [1] "jimmy" "150"
##
## [[2]]
## [1] "nicker" "140"
##
## [[3]]
## [1] "tony" "152"
str_split(y," ",simplify = T)#按空格拆分,此时是矩阵,可用as.dataframe转化为数据框
## [,1] [,2]
## [1,] "jimmy" "150"
## [2,] "nicker" "140"
## [3,] "tony" "152"
### 3.按位置提取字符串
str_sub(x,5,9)#5到9位的字符串
## [1] "birch"
### 4.字符检测
str_detect(x2,"h")#检测元素中各字符串是否含有关键词,含h的字符串为T,结合ifelse函数可以取出含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.字符串替换
x2
## [1] "The" "birch" "canoe" "slid" "on"
## [6] "the" "smooth" "planks."
str_replace(x2,"o","A")#smooth第二个o没有替换掉,只替换第一个o
## [1] "The" "birch" "canAe" "slid" "An"
## [6] "the" "smAoth" "planks."
str_replace_all(x2,"o","A")#另一个函数替换所有o
## [1] "The" "birch" "canAe" "slid" "An"
## [6] "the" "smAAth" "planks."
### 6.字符删除
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."
test <- iris[c(1:2,51:52,101:102),]
rownames(test) =NULL # 去掉行名,NULL是“什么都没有”,本来的行名是1,2,51,52,101,102
test
## Sepal.Length Sepal.Width Petal.Length Petal.Width
## 1 5.1 3.5 1.4 0.2
## 2 4.9 3.0 1.4 0.2
## 3 7.0 3.2 4.7 1.4
## 4 6.4 3.2 4.5 1.5
## 5 6.3 3.3 6.0 2.5
## 6 5.8 2.7 5.1 1.9
## Species
## 1 setosa
## 2 setosa
## 3 versicolor
## 4 versicolor
## 5 virginica
## 6 virginica
####2玩转数据框
# arrange,数据框按照某一列排序(排序的是整个数据框),所以不用sort(只能一列向量)
#注释order()函数只能用于向量和数组,而不能用于数据框。要按顺序排列数据框,请使用arrange()函数。
library(dplyr)
arrange(test, Sepal.Length) #从小到大
## Sepal.Length Sepal.Width Petal.Length Petal.Width
## 1 4.9 3.0 1.4 0.2
## 2 5.1 3.5 1.4 0.2
## 3 5.8 2.7 5.1 1.9
## 4 6.3 3.3 6.0 2.5
## 5 6.4 3.2 4.5 1.5
## 6 7.0 3.2 4.7 1.4
## Species
## 1 setosa
## 2 setosa
## 3 virginica
## 4 virginica
## 5 versicolor
## 6 versicolor
arrange(test, desc(Sepal.Length)) #最后一列从大到小,arrange(df, desc(df[,ncol(df)]))
## Sepal.Length Sepal.Width Petal.Length Petal.Width
## 1 7.0 3.2 4.7 1.4
## 2 6.4 3.2 4.5 1.5
## 3 6.3 3.3 6.0 2.5
## 4 5.8 2.7 5.1 1.9
## 5 5.1 3.5 1.4 0.2
## 6 4.9 3.0 1.4 0.2
## Species
## 1 versicolor
## 2 versicolor
## 3 virginica
## 4 virginica
## 5 setosa
## 6 setosa
# distinct,数据框按照某一列去重复
distinct(test,Species,.keep_all = T)##.keep_all=T保留数据框
## Sepal.Length Sepal.Width Petal.Length Petal.Width
## 1 5.1 3.5 1.4 0.2
## 2 7.0 3.2 4.7 1.4
## 3 6.3 3.3 6.0 2.5
## Species
## 1 setosa
## 2 versicolor
## 3 virginica
# mutate,数据框新增一列
mutate(test, new = Sepal.Length * Sepal.Width)#此时dim(test)还是5列,因为没有赋值就没有修改
## Sepal.Length Sepal.Width Petal.Length Petal.Width
## 1 5.1 3.5 1.4 0.2
## 2 4.9 3.0 1.4 0.2
## 3 7.0 3.2 4.7 1.4
## 4 6.4 3.2 4.5 1.5
## 5 6.3 3.3 6.0 2.5
## 6 5.8 2.7 5.1 1.9
## Species new
## 1 setosa 17.85
## 2 setosa 14.70
## 3 versicolor 22.40
## 4 versicolor 20.48
## 5 virginica 20.79
## 6 virginica 15.66
# 连续的步骤
# 1.多次赋值,产生多个中间的变量
x1 = select(iris,-5)#除第5行,其余全曲
x2 = as.matrix(x1)
x3 = head(x2,50)#取前50行
pheatmap::pheatmap(x3)
#注释select,filter筛选列行。R语言中,select函数用于选择数据框中的列,可以使用列名或者向量来指定要选择的列。例如,若要选择数据框df中的列x和y,可以使用以下代码:library(dplyr),df <- select(df, x, y),filter函数用于筛选数据框中的行,它接受一个逻辑条件作为参数,返回符合条件的行。例如,若要选出数据框df中满足条件x>10且y<5的行,可以使用以下代码:df <- filter(df, x > 10 & y < 5),与select函数类似,filter也可以使用多个逻辑条件组合,进行更加复杂的筛选操作。
# 2. 嵌套,代码不易读
pheatmap::pheatmap(head(as.matrix(select(iris,-5)),50))
# 3.管道符号%>%(ctrl+shift+m)传递,简洁明了
iris %>%
select(-5) %>%#第一个参数空着,放到了第一行
as.matrix() %>%
head(50) %>%
pheatmap::pheatmap()
####条件语句
rm(list = ls())
## 一.条件语句
###1.if(){ }
#### (1)只有if没有else,那么条件是FALSE时就什么都不做
i = -1
if (i<0) print('up')
## [1] "up"
if (i>0) print('up')
#理解下面代码
if(!require(tidyr)) install.packages('tidyr') #无包则装包
#长脚本管理1:通过if语句的{}折叠功能
#if (F) {
+ #把不运行的代码括起来
# + }
#if (T) {
+ #把运行的代码括起来
# + }
#长脚本管理2:分成多个脚本,每个脚本最后保存Rdata,下个脚本清空再加载
#### (2)有else
i =1
## Error in ++i = 1: could not find function "+<-"
if (i>0){
print('+')
} else {
print("-")
}
## [1] "-"
i = 1
ifelse(i>0,"+","-")#三个参数,ifelse(x,<yes>,<no>)
## [1] "+"
x = rnorm(3)
x
## [1] -0.51225515 0.85354436 0.01278204
ifelse(x>0,"+","-")
## [1] "-" "+" "+"
#ifelse()+str_detect(),王炸
samples = c("tumor1","tumor2","tumor3","normal1","normal2","normal3")
k1 = str_detect(samples,"tumor");k1##检测向量中元素有无某关键词
## [1] TRUE TRUE TRUE FALSE FALSE FALSE
ifelse(k1,"tumor","normal")
## [1] "tumor" "tumor" "tumor" "normal" "normal" "normal"
k2 = str_detect(samples,"normal");k2
## [1] FALSE FALSE FALSE TRUE TRUE TRUE
ifelse(k2,"normal","tumor")
## [1] "tumor" "tumor" "tumor" "normal" "normal" "normal"
#### (3)多个条件
i = 0
if (i>0){
print('+')
} else if (i==0) {
print('0')
} else if (i< 0){
print('-')
}
## [1] "0"
ifelse(i>0,"+",ifelse(i<0,"-","0"))
## [1] "0"
## 二、for循环
for( i in 1:4){
print(paste0("the current number is ",i))
}
## [1] "the current number is 1"
## [1] "the current number is 2"
## [1] "the current number is 3"
## [1] "the current number is 4"
#批量画图
par(mfrow = c(2,2))
for(i in 1:4){
print(paste0("the current column is ",colnames(iris)[i]))
plot(iris[,i],col = iris[,5])
}
## [1] "the current column is Sepal.Length"
## [1] "the current column is Sepal.Width"
## [1] "the current column is Petal.Length"
## [1] "the current column is Petal.Width"
#批量装包
pks = c("tidyr","dplyr","stringr")
for(g in pks){
print(paste0("package ",g," is installing"))
if(!require(g,character.only = T))
install.packages(g,ask = F,update = F)
}
## [1] "package tidyr is installing"
## [1] "package dplyr is installing"
## [1] "package stringr is installing"
#练习7-1----
# 1.读取group.csv,从第二列中提取圈出来的信息
# 2.如何把上一题结果中的Control和Vemurafenib改成全部小写?搜索一下
# 3.加载test1.Rdata,按照symbol列给ids数据框去重复,注意,要让ids数据真正发生修改。
library(stringr)
a = read.csv("group.csv")
g = str_split(a$title," ",simplify = T)
g
## [,1] [,2] [,3] [,4] [,5]
## [1,] "A375" "cells" "24h" "Control" "rep1"
## [2,] "A375" "cells" "24h" "Control" "rep2"
## [3,] "A375" "cells" "24h" "Control" "rep3"
## [4,] "A375" "cells" "24h" "Vemurafenib" "rep1"
## [5,] "A375" "cells" "24h" "Vemurafenib" "rep2"
## [6,] "A375" "cells" "24h" "Vemurafenib" "rep3"
g[,4]
## [1] "Control" "Control" "Control" "Vemurafenib"
## [5] "Vemurafenib" "Vemurafenib"
# 2.如何把上一题结果中的Control和Vemurafenib改成全部小写?搜索一下
tolower(g[,4])
## [1] "control" "control" "control" "vemurafenib"
## [5] "vemurafenib" "vemurafenib"
str_to_lower(g[,4])#注释1在R语言中,str_to_lower()函数是由stringr包提供的函数,而tolower()函数是R的基础函数之一。这两个函数都用于将字符串转换为小写字母,但是它们有以下区别:库依赖:str_to_lower()需要先安装和加载stringr包,而tolower()是R的内置函数,不需要任何额外的库。输入类型:str_to_lower()仅适用于字符向量或字符串标量,而tolower()可以应用于所有字符数据类型,例如字符向量、字符串标量、因子等。输出类型:str_to_lower()始终返回字符向量,而tolower()根据输入变量类型的不同而返回不同类型的结果。总的来说,两个函数都可以用于将字符串转换为小写字母,但str_to_lower()更具特定性,而tolower()则更通用且直接可用。
## [1] "control" "control" "control" "vemurafenib"
## [5] "vemurafenib" "vemurafenib"
# 3.加载test1.Rdata,按照symbol列给ids数据框去重复,注意,要让ids数据真正发生修改。
load("test1.Rdata")
nrow(ids)
## [1] 19827
ids = distinct(ids,symbol,.keep_all = T)
nrow(ids)
## [1] 18834
#练习7-2----
# 1.加载deg.Rdata,根据a、b两列的值,按照以下条件生成向量x:
#a< -1 且b<0.05,则x对应的值为down;
#a>1 且b<0.05,则x对应的值为up;
#其他情况,x对应的值为no
# 统计up、down、no各出现了多少次
# 提示:ifelse函数
y3 <- filter(deg, a < -1 & b < 0.05);table(y3)
```r
x3 <- deg$a< -1°$b<0.05;table(x3)
```r
y4 <- filter(deg, a>1 & b < 0.05);table(y4)
```r
x4 <- deg$a>1°$b<0.05;table(x4)
```r
x <- ifelse(x3,"down",ifelse(x4,"up","no"));table(x)
```r
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
```r
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"),#pivot_longer宽变长;ggplot只能把接受数据在一列
names_to = "gene", #把列名变成一列"gene"列
values_to = "count")#对应的数值加到"count"列
library(ggplot2)
p = ggplot(pdat,aes(gene,count))+
geom_boxplot(aes(fill = group))+
theme_bw()
p
![plot of chunk unnamed-chunk-1](figure/unnamed-chunk-1-5.png)
```r
p + facet_wrap(~gene,scales = "free") #按基因分面
![plot of chunk unnamed-chunk-1](figure/unnamed-chunk-1-6.png)
```r
#5隐式循环apply
rm(list = ls())
#apply(X, MARGIN, FUN, …)
#其中X是数据框/矩阵名;
#MARGIN为1表示行,为2表示列,FUN是函数
test<- iris1:6,1:4
apply(test, 2, mean)#数据框每列求平均值,继承了列名,但还是数值型向量
```r
apply(test, 1, sum)#数据框每行加和
```r
par(mfrow = c(2,2))
apply(iris,1:4, 2, plot)#每一列画图
![plot of chunk unnamed-chunk-1](figure/unnamed-chunk-1-7.png)
```r
#如果有写不下的参数,继续写在apply的括号里
apply(iris,1:4, 2, plot,col = iris,5)
![plot of chunk unnamed-chunk-1](figure/unnamed-chunk-1-8.png)
```r
#或者也可以自定义函数
jimmy <- function(g){
plot(g,col = iris,5)
}
par(mfrow = c(2,2))
apply(iris,1:4, 2, jimmy)
![plot of chunk unnamed-chunk-1](figure/unnamed-chunk-1-9.png)
```r
#思考1,30个数,挑最大的5个
x <- rnorm(30);x
```r
tail(sort(x),5)
```r
#思考2,挑出一个表达矩阵里方差最大的1000个基因,转换为数据框取
load("test2.Rdata")
class(test)#矩阵
```r
z <- as.data.frame(test)#转换为数据框
z <- mutate(z,var=apply(z, 1, sd))#为每行取方差,增加到最后一列
z <- rownames_to_column(z)#将行名作为第一列,不可先加行名,否则每行取方差会显示NA,因为数据类型会不一样
dim(z)
```r
z <- arrange(z, desc(z,ncol(z)))#按照最后一列给整个数据框排序
z$rowname1:1000
#取方差最大的1000个基因名,2矩阵直接取完美答案
load("test2.Rdata")
apply(test,1,var)
tail(sort(apply(test,1,var)),1000)
```r
names(tail(sort(apply(test,1,var)),1000))
```r
#管道符号
test %>%
apply(1, var) %>%
sort() %>%
tail(1000) %>%
names()
#注释在R语言中,head()和tail()函数用于查看数据框或向量的前几行或后几行。默认情况下,head()函数显示数据框的前6行,而tail()函数显示数据框的后6行。可以使用可选参数n来指定要显示的行数。例如,假设有一个名为mydata的数据框,我们可以使用以下代码来查看前3行和后3行:
# 查看前3行head(mydata, n = 3)
# 查看后3行tail(mydata, n = 3)注意,在这里,n是一个可选参数,它指定要显示的行数。如果不指定n,则默认值为6。
### 2.lapply(list, FUN, …)
# 对列表/向量中的每个元素实施相同的操作
lapply(1:4,rnorm)
## [[1]]
## [1] -2.264267
##
## [[2]]
## [1] -0.6492101 -0.6616064
##
## [[3]]
## [1] -0.08256421 -0.17671407 0.10540791
##
## [[4]]
## [1] -0.3678018 0.9070279 -0.3279892 0.2457803
#批量画图
lapply(1:4, function(i){
plot(iris[,i],col = iris[,5])
})
## [[1]]
## NULL
##
## [[2]]
## NULL
##
## [[3]]
## NULL
##
## [[4]]
## NULL
#批量装包
pks = c("tidyr","dplyr","stringr")
qa = function(g){
print(paste0("package ",g," is installing"))
if(!require(g,character.only = T))
install.packages(g,ask = F,update = F)
}
lapply(pks, qa)
## [1] "package tidyr is installing"
## [1] "package dplyr is installing"
## [1] "package stringr is installing"
## [[1]]
## NULL
##
## [[2]]
## NULL
##
## [[3]]
## NULL
#6两个数据框的连接
test1 <- data.frame(name = c('jimmy','nicker','Damon','Sophie'),
blood_type = c("A","B","O","AB"))
test1
## name blood_type
## 1 jimmy A
## 2 nicker B
## 3 Damon O
## 4 Sophie AB
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
## name group vision
## 1 Damon group1 4.2
## 2 jimmy group1 4.3
## 3 nicker group2 4.9
## 4 tony group2 4.5
library(dplyr)
inner_join(test1,test2,by="name")#取交集连接
## name blood_type group vision
## 1 jimmy A group1 4.3
## 2 nicker B group2 4.9
## 3 Damon O group1 4.2
right_join(test1,test2,by="name")#右连接
## name blood_type group vision
## 1 jimmy A group1 4.3
## 2 nicker B group2 4.9
## 3 Damon O group1 4.2
## 4 tony <NA> group2 4.5
full_join(test1,test2,by="name")
## name blood_type group vision
## 1 jimmy A group1 4.3
## 2 nicker B group2 4.9
## 3 Damon O group1 4.2
## 4 Sophie AB <NA> NA
## 5 tony <NA> group2 4.5
semi_join(test1,test2,by="name")
## name blood_type
## 1 jimmy A
## 2 nicker B
## 3 Damon O
anti_join(test1,test2,by="name")
## name blood_type
## 1 Sophie AB
#一些好用的函数
# 1.match-----
load("matchtest.Rdata")
x
## file_name
## 1 708a16a3-7a5e-4e27-b06b-4c3c308b11fe.htseq.counts.gz
## 2 95e726db-5ccc-4836-a2ae-7feaddaf9f1b.htseq.counts.gz
## 3 90a46dce-5762-47ec-925c-deff853069aa.htseq.counts.gz
## 4 587e44e4-87ba-4981-a520-d20612486f53.htseq.counts.gz
## 5 1b843dbb-5ef0-47ca-9783-dbeb94aa6df3.htseq.counts.gz
## 6 09796233-3f40-4deb-b77d-2267c3afff59.htseq.counts.gz
## 7 44f1dc34-a01e-4a7b-a7a1-a90064039fdd.htseq.counts.gz
## ID
## 1 TCGA-AA-3531-01A-01R-0821-07
## 2 TCGA-A6-2678-11A-01R-A32Z-07
## 3 TCGA-AA-A02K-01A-03R-A32Y-07
## 4 TCGA-NH-A6GA-01A-11R-A37K-07
## 5 TCGA-AZ-6600-11A-01R-1774-07
## 6 TCGA-CM-6676-01A-11R-1839-07
## 7 TCGA-AA-3971-01A-01R-1022-07
y
## 90a46dce-5762-47ec-925c-deff853069aa.htseq.counts.gz
## ENSG00000000003.13 6564
## ENSG00000000005.5 29
## ENSG00000000419.11 2659
## ENSG00000000457.12 246
## ENSG00000000460.15 145
## ENSG00000000938.11 37
## ENSG00000000971.14 77
## 587e44e4-87ba-4981-a520-d20612486f53.htseq.counts.gz
## ENSG00000000003.13 3127
## ENSG00000000005.5 0
## ENSG00000000419.11 889
## ENSG00000000457.12 382
## ENSG00000000460.15 188
## ENSG00000000938.11 749
## ENSG00000000971.14 708
## 95e726db-5ccc-4836-a2ae-7feaddaf9f1b.htseq.counts.gz
## ENSG00000000003.13 6330
## ENSG00000000005.5 0
## ENSG00000000419.11 2428
## ENSG00000000457.12 1701
## ENSG00000000460.15 1009
## ENSG00000000938.11 220
## ENSG00000000971.14 530
## 09796233-3f40-4deb-b77d-2267c3afff59.htseq.counts.gz
## ENSG00000000003.13 3583
## ENSG00000000005.5 70
## ENSG00000000419.11 1436
## ENSG00000000457.12 590
## ENSG00000000460.15 440
## ENSG00000000938.11 812
## ENSG00000000971.14 4173
## 708a16a3-7a5e-4e27-b06b-4c3c308b11fe.htseq.counts.gz
## ENSG00000000003.13 643
## ENSG00000000005.5 17
## ENSG00000000419.11 2476
## ENSG00000000457.12 804
## ENSG00000000460.15 496
## ENSG00000000938.11 962
## ENSG00000000971.14 3958
## 44f1dc34-a01e-4a7b-a7a1-a90064039fdd.htseq.counts.gz
## ENSG00000000003.13 1514
## ENSG00000000005.5 13
## ENSG00000000419.11 876
## ENSG00000000457.12 483
## ENSG00000000460.15 250
## ENSG00000000938.11 75
## ENSG00000000971.14 272
## 1b843dbb-5ef0-47ca-9783-dbeb94aa6df3.htseq.counts.gz
## ENSG00000000003.13 11751
## ENSG00000000005.5 26
## ENSG00000000419.11 2494
## ENSG00000000457.12 531
## ENSG00000000460.15 632
## ENSG00000000938.11 85
## ENSG00000000971.14 319
## 如何把y的列名正确替换为x里面的ID?
## (1)分步解法
a = colnames(y)
b = x$file_name
k = match(a,b);k
## [1] 3 4 2 6 1 7 5
#match(a,b)的意思是a里的每个元素在b的第几个位置上。
#是b的下标,可以给b取子集,也可以给与b对应的其他向量取子集。
colnames(y) = x$ID[k]
## (2)一步解法
load("matchtest.Rdata")
colnames(y) = x$ID[match(colnames(y),x$file_name)]
## (3)放弃match的解法
load("matchtest.Rdata")
rownames(x) = x$file_name
x = x[colnames(y),]
colnames(y) = x$ID
# 2.一些搞文件的函数----
dir() # 列出工作目录下的文件
## [1] "0_pre_install.R" "1_玩转字符串.R"
## [3] "1string的6个基础函数.png" "2_玩转数据框.R"
## [5] "3_条件和循环.R" "4_表达矩阵画箱线图.R"
## [7] "5_隐式循环.R" "6_两个数据框的连接.R"
## [9] "7_一些顶呱呱的函数.R" "8_exercise.R"
## [11] "9_anwser.R" "deg.Rdata"
## [13] "figure" "group.csv"
## [15] "matchtest.Rdata" "note5.1.Rmd"
## [17] "note5.Rmd" "R_04.Rproj"
## [19] "test1.Rdata" "test2.Rdata"
dir(pattern = ".R$") #列出工作目录下以.R结尾的文件
## [1] "0_pre_install.R" "1_玩转字符串.R"
## [3] "2_玩转数据框.R" "3_条件和循环.R"
## [5] "4_表达矩阵画箱线图.R" "5_隐式循环.R"
## [7] "6_两个数据框的连接.R" "7_一些顶呱呱的函数.R"
## [9] "8_exercise.R" "9_anwser.R"
file.create("douhua.txt") #用代码创建文件
## [1] TRUE
file.exists("douhua.txt") #某文件在工作目录下是否存在
## [1] TRUE
file.remove("douhua.txt") #用代码删除文件
## [1] TRUE
file.exists("douhua.txt") #删掉了就不存在啦
## [1] FALSE
## 可以批量的新建和删除
f = paste0("douhua",1:100,".txt")
file.create(f)
## [1] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
## [12] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
## [23] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
## [34] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
## [45] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
## [56] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
## [67] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
## [78] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
## [89] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
## [100] TRUE
file.remove(f)
## [1] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
## [12] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
## [23] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
## [34] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
## [45] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
## [56] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
## [67] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
## [78] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
## [89] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
## [100] TRUE
引用生信技能数 This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.
When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
summary(cars)
## speed dist
## Min. : 4.0 Min. : 2.00
## 1st Qu.:12.0 1st Qu.: 26.00
## Median :15.0 Median : 36.00
## Mean :15.4 Mean : 42.98
## 3rd Qu.:19.0 3rd Qu.: 56.00
## Max. :25.0 Max. :120.00
Note that the echo = FALSE
parameter was added to the code chunk to prevent printing of the R code that generated the plot.
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。