前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Day7-R语言综合运用

Day7-R语言综合运用

原创
作者头像
用户11008504
发布2024-05-08 01:21:20
660
发布2024-05-08 01:21:20
举报
文章被收录于专栏:生信马拉松生信马拉松

玩转字符串

字符型向量:由字符串元素组成的向量

字符串:一个引号里的所有东西

字符:引号里的单个字母/数字/符合

需安装stringr包

长度:str_length()

length()计算的是字符串的个数

str_length()计算字符串里字符的个数

代码语言:R
复制
x <- "The birch canoe slid on the smooth planks."
x
[1] "The birch canoe slid on the smooth planks."
 ### 1.检测字符串长度
str_length(x)
[1] 42
length(x)
[1] 1

拆分:str_split()

2.字符串拆分

代码语言:R
复制
str_split(x," ")
[[1]]
[1] "The"     "birch"   "canoe"   "slid"    "on"      "the"     "smooth"  "planks."
class(str_split(x," "))
[1] "list"
x2 = str_split(x," ")[[1]];x2
[1] "The"     "birch"   "canoe"   "slid"    "on"      "the"     "smooth"  "planks."
y = c("jimmy 150","nicker 140","tony 152")
str_split(y," ")
[[1]]
[1] "jimmy" "150"  
[[2]]
[1] "nicker" "140"   
[[3]]
[1] "tony" "152" 
str_split(y," ",simplify = T)
     [,1]     [,2] 
[1,] "jimmy"  "150"
[2,] "nicker" "140"
[3,] "tony"   "152"

按位置提取

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

字符检测

代码语言:R
复制
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

字符替换

代码语言:r
复制
 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"     "smAAth"  "planks."

字符删除

代码语言:R
复制
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."
stringr常用知识点总结
stringr常用知识点总结

玩转数据框

arrange,数据框按照某一列排序

代码语言:R
复制
test
  Sepal.Length Sepal.Width Petal.Length Petal.Width    Species
1          5.1         3.5          1.4         0.2     setosa
2          4.9         3.0          1.4         0.2     setosa
3          7.0         3.2          4.7         1.4 versicolor
4          6.4         3.2          4.5         1.5 versicolor
5          6.3         3.3          6.0         2.5  virginica
6          5.8         2.7          5.1         1.9  virginica
> library(dplyr)
> arrange(test, Sepal.Length) #从小到大
  Sepal.Length Sepal.Width Petal.Length Petal.Width    Species
1          4.9         3.0          1.4         0.2     setosa
2          5.1         3.5          1.4         0.2     setosa
3          5.8         2.7          5.1         1.9  virginica
4          6.3         3.3          6.0         2.5  virginica
5          6.4         3.2          4.5         1.5 versicolor
6          7.0         3.2          4.7         1.4 versicolor

distinct,数据框按照某一列去重复

代码语言:R
复制
distinct(test,Species,.keep_all = T)
  Sepal.Length Sepal.Width Petal.Length Petal.Width    Species
1          5.1         3.5          1.4         0.2     setosa
2          7.0         3.2          4.7         1.4 versicolor
3          6.3         3.3          6.0         2.5  virginica

mutate,数据框新增一列

代码语言:R
复制
mutate(test, new = Sepal.Length * Sepal.Width)
  Sepal.Length Sepal.Width Petal.Length Petal.Width    Species   new
1          5.1         3.5          1.4         0.2     setosa 17.85
2          4.9         3.0          1.4         0.2     setosa 14.70
3          7.0         3.2          4.7         1.4 versicolor 22.40
4          6.4         3.2          4.5         1.5 versicolor 20.48
5          6.3         3.3          6.0         2.5  virginica 20.79
6          5.8         2.7          5.1         1.9  virginica 15.66

管道符号 %>%

前一步的输出作为后面的输入,传到第一参数位置

条件语句

if(一个逻辑值,不可以是多个逻辑值组成的向量){代码}

(1)只有if没有else,那么条件是FALSE时就什么都不做

代码语言:r
复制
i = -1
if (i<0) print('up')
[1] "up"
if (i>0) print('up')

if条件语句:如果...就...否则

代码语言:R
复制
i =1
if (i>0){
+   print('+')
+ } else {
+   print("-")
+ }
[1] "+"

ifelse函数

3个参数:ifels(x,yes,no)

x: 逻辑值或逻辑向量

yes: 逻辑值为TRUE时返回值

no: 逻辑值为FALSE时返回值

代码语言:r
复制
i = 1
ifelse(i>0,"+","-")
[1] "+"

* ifelse()+str_detect(),王炸

if多个条件

代码语言:R
复制
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循环

对x里的每一个元素i进行同一操作

for( i in x){代码}

代码语言:r
复制
for( i in 1:4){
  print(i)
}
[1] 1
[1] 2
[1] 3
[1] 4

矩阵/数据框的隐式循坏-apply

apply(X, MARGIN, FUN, …)

其中X是数据框/矩阵名;

MARGIN为1表示行,为2表示列,FUN是函数

对X的每一行/列进行FUN这个函数

代码语言:r
复制
test<- iris[1:6,1:4]
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          4.7         3.2          1.3         0.2
4          4.6         3.1          1.5         0.2
5          5.0         3.6          1.4         0.2
6          5.4         3.9          1.7         0.4
apply(test, 2, mean)
Sepal.Length  Sepal.Width Petal.Length  Petal.Width 
   4.9500000    3.3833333    1.4500000    0.2333333 
apply(test, 1, sum)
   1    2    3    4    5    6 
10.2  9.5  9.4  9.4 10.2 11.4 

向量/列表的隐式循环lapply(list, FUN, …)

lapply(list, FUN, …) :对列表/向量中的每个元素实施相同的操作

两个数据框连接

inner_join():交集

full_join(): 全连接

left_join():左连接

right_join():右连接

表达矩阵画箱线图

一些好用的函数

代码语言:r
复制
 1.match-----
load("matchtest.Rdata")
x
y
## 如何把y的列名正确替换为x里面的ID?

## (1)分步解法
a = colnames(y)
b = x$file_name
k = match(a,b);k
#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() # 列出工作目录下的文件
dir(pattern = ".R$") #列出工作目录下以.R结尾的文件

file.create("douhua.txt") #用代码创建文件
file.exists("douhua.txt") #某文件在工作目录下是否存在
file.remove("douhua.txt") #用代码删除文件
file.exists("douhua.txt") #删掉了就不存在啦

## 可以批量的新建和删除
f = paste0("douhua",1:100,".txt")
file.create(f)
file.remove(f)

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 玩转字符串
    • 长度:str_length()
      • 拆分:str_split()
        • 2.字符串拆分
      • 按位置提取
        • 字符检测
          • 字符替换
            • 字符删除
            • 玩转数据框
              • arrange,数据框按照某一列排序
                • distinct,数据框按照某一列去重复
                  • mutate,数据框新增一列
                    • 管道符号 %>%
                    • 条件语句
                      • ifelse函数
                        • * ifelse()+str_detect(),王炸
                          • if多个条件
                          • for循环
                          • 矩阵/数据框的隐式循坏-apply
                          • 向量/列表的隐式循环lapply(list, FUN, …)
                          • 两个数据框连接
                            • inner_join():交集
                              • full_join(): 全连接
                                • left_join():左连接
                                  • right_join():右连接
                                  • 表达矩阵画箱线图
                                  • 一些好用的函数
                                  领券
                                  问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
                                  http://www.vxiaotou.com