前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Day 6_学习R包- CG

Day 6_学习R包- CG

原创
作者头像
Crazy_George
修改2024-03-29 13:25:38
1100
修改2024-03-29 13:25:38
举报
文章被收录于专栏:一周生信入门一周生信入门
R包(Packages)
R包(Packages)

R包是多个函数的集合,具有详细的说明和示例,help(R包)


1. R包安装和加载

1.1 镜像设置

运行代码:

代码语言:r
复制
options("repos" = c(CRAN="https://mirrors.tuna.tsinghua.edu.cn/CRAN/")) #对应清华源
options(BioC_mirror="https://mirrors.ustc.edu.cn/bioc/") #对应中科大源

1.2 R包安装命令

  • 安装的包安装在CRAN:使用代码install.packages("包")
  • 安装的包在bioconductor:使用代码BiocManager::install("包")

1.3 dplyr包安装和加载

代码语言:r
复制
install.packages("dplyr")#镜像设置后使用代码
library(dplyr)#使用require()命令也可加载包

2. dplyr基础函数

示例数据:内置数据iris简化版

test <- iris[c(1:2,51:52,101:102),]


2.1 新增列,mutate()

代码语言:r
复制
mutate(test, new = Sepal.Length*Sepal.Width)#在变量test的数据框新增列,列名是new,数值是Sepal.Length列的值和Sepal.Width列的值相乘。

问题和思考

在我练习select()时,想选择刚新增的列,发现报错。然后发现运行mutate(test, new = Sepal.Length*Sepal.Width)后,查看test后发现test本身没有变。

因此我想新增列只是一个操作,不会使变量test本身多一列,若想要对test数据框真实多一列,需重新对test进行赋值,具体如下:

test <- mutate(test, new = Sepal.Length*Sepal.Width),然后查看变量test,test多了名为new的列。

2.2 按列筛选select()

2.2.1 按列号筛选

代码语言:r
复制
select(test,6)#按列号筛选
  #    new
#1   17.85
#2   14.70
#51  22.40
#52  20.48
#101 20.79
#102 15.66
select(test,c(1,4))#按向量包含的列号筛选
#    Sepal.Length Petal.Width
#1            5.1         0.2
#2            4.9         0.2
#51           7.0         1.4
#52           6.4         1.5
#101          6.3         2.5
#102          5.8         1.9
 select(test,Species)#按列名筛选
 #      Species
#1       setosa
#2       setosa
#51  versicolor
#52  versicolor
#101  virginica
#102  virginica

2.2.2 按列名筛选

代码语言:r
复制
select(test,Sepal.Length,Sepal.Width)
#    Sepal.Length Sepal.Width
#1            5.1         3.5
#2            4.9         3.0
#51           7.0         3.2
#52           6.4         3.2
#101          6.3         3.3
#102          5.8         2.7
代码语言:r
复制
vars <- c("Petal.Length","Petal.Width")#赋值
vars#变量
select(test,one_of(vars))
select(test,vars)
select(test,c("Petal.Length","Petal.Width"))

2.3 筛选行filter()

代码语言:r
复制
filter(test,Species == "setosa")
#  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
filter(test,Species == "setosa"&Sepal.Length > 5)
#  Sepal.Length Sepal.Width Petal.Length Petal.Width Species   new
#1          5.1         3.5          1.4         0.2  setosa 17.85
filter(test,Species %in% c(c("setosa","virginica")))
#  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          6.3         3.3          6.0         2.5 virginica 20.79
#4          5.8         2.7          5.1         1.9 virginica 15.66

##2.4 按某一列或某几列对整个表格进行排序

代码语言:r
复制
arrange(test,Sepal.Width)#默认从小到大排序
#  Sepal.Length Sepal.Width Petal.Length Petal.Width    Species   new
#1          5.8         2.7          5.1         1.9  virginica 15.66
#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.1         3.5          1.4         0.2     setosa 17.85
arrange(test,desc(Sepal.Width))#从大到小需要用到desc
# Sepal.Length Sepal.Width Petal.Length Petal.Width    Species   new
#1          5.1         3.5          1.4         0.2     setosa 17.85
#2          6.3         3.3          6.0         2.5  virginica 20.79
#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          4.9         3.0          1.4         0.2     setosa 14.70
#6          5.8         2.7          5.1         1.9  virginica 15.66

2.5 汇总summarise()

对数据进行汇总操作,结合group_by体验强实用性

代码语言:r
复制
summarise(test,mean(Sepal.Width),sd(Petal.Width))
##  mean(Sepal.Width) sd(Petal.Width)
##1              3.15       0.9239408
group_by(test,Species)
# A tibble: 6 × 6
# Groups:   Species [3]
#  Sepal.Length Sepal.Width Petal.Length Petal.Width Species      new
#         <dbl>       <dbl>        <dbl>       <dbl> <fct>      <dbl>
#1          5.1         3.5          1.4         0.2 setosa      17.8
#2          4.9         3            1.4         0.2 setosa      14.7
#3          7           3.2          4.7         1.4 versicolor  22.4
#4          6.4         3.2          4.5         1.5 versicolor  20.5
#5          6.3         3.3          6           2.5 virginica   20.8
#6          5.8         2.7          5.1         1.9 virginica   15.7
 summarise(group_by(test,Species),mean(Sepal.Width),sd(Petal.Width))
# A tibble: 3 × 3
#  Species    `mean(Sepal.Width)` `sd(Petal.Width)`
#  <fct>                    <dbl>             <dbl>
#1 setosa                    3.25            0     
#2 versicolor                3.2             0.0707
#3 virginica                 3               0.424 

3. dplyr的两个实用技能

3.1 管道操作%>% (cmd/ctl + shift +M)

理解如下命令,以后多练习

代码语言:r
复制
test %>% 
+   group_by(Species) %>% 
+   summarise(mean(Sepal.Length), sd(Sepal.Length))
# A tibble: 3 × 3
#  Species    `mean(Sepal.Length)` `sd(Sepal.Length)`
#  <fct>                     <dbl>              <dbl>
#1 setosa                     5                 0.141
#2 versicolor                 6.7               0.424
#3 virginica                  6.05              0.354

3.2 统计某列的unique值count()

代码语言:r
复制
count()
count(test,Species)#统计变量test的Species列中每个元素出现次数(unique)
#Species n
#1     setosa 2
#2 versicolor 2
#3  virginica 2

4. dplyr处理关系数据(将两个数据框连接)

4.1 inner_join(内连,取交集)

代码语言:r
复制
test1 <- data.frame(x = c("a","b","c","d"),z = c("1","2","3","4"))
 test2 <- data.frame(x = c("a","c","d","x","y","z"),y = c("A","B","C","D","E","F"))
 test1
#  x z
#1 a 1
#2 b 2
#3 c 3
#4 d 4
 test2
#  x y
#1 a A
#2 c B
#3 d C
#4 x D
#5 y E
#6 z F
 inner_join(test1,test2,by = "x")#提取出对变量test1、test2中列名相同的列中相同的元素的行,合并到一个数据框(test1在左,test2在右)。
#  x z y
#1 a 1 A
#2 c 3 B
#3 d 4 C

4.2 left_join,左连

代码语言:r
复制
> left_join(test1,test2,by = "x")#test1在左,test2取x列中和test1中x列有交集的置于test1右侧,无交集的现实<NA>
#  x z    y
#1 a 1    A
#2 b 2 <NA>
#3 c 3    B
#4 d 4    C
> left_join(test2,test1,by = "x")#test2在左,test1取x列中和test1中x列有交集的置于test2右侧,无交集的现实<NA>
#  x y    z
#1 a A    1
#2 c B    3
#3 d C    4
#4 x D <NA>
#5 y E <NA>
#6 z F <NA>

4.3 full.join全连

代码语言:r
复制
> full_join(test1,test2,by = "x")
#  x    z    y
#1 a    1    A
#2 b    2 <NA>
#3 c    3    B
#4 d    4    C
#5 x <NA>    D
#6 y <NA>    E
#7 z <NA>    F
> full_join(test2,test1,by = "x")#test1和test2前后互换一下看看区别
#  x    y    z
#1 a    A    1
#2 c    B    3
#3 d    C    4
#4 x    D <NA>
#5 y    E <NA>
#6 z    F <NA>
#7 b <NA>    2

4.4 semi_join: 半连接

代码语言:r
复制
> semi_join(x = test1,y = test2,by = "x")
#  x z
#1 a 1
#2 c 3
#3 d 4
> semi_join(x = test2,y = test1,by = "x")
#  x y
#1 a A
#2 c B
#3 d C

4.5 anti_join:返回无法和y表相匹配的x表所有记录

代码语言:r
复制
> anti_join(x = test1,y = test2,by = "x")
#  x z
#1 b 2
> anti_join(x = test2,y = test1,by = "x")
#  x y
#1 x D
#2 y E
#3 z F

4.6 简单合并

  • bind_rows():按照行合并两个数据框,两个数据框列数必须相同。
  • bind_cols():按照列合并两个数据框,两个数据框行数必须相同。
  • 搞清base包的cbind()、rbind()和bind_rows()、bind_cols()的区别。
代码语言:r
复制
> test1 <- data.frame(x = c(11,12,13,14), y = c(11,22,33,44))
> test2 <- data.frame(x = c(33.33,66.66), y = c(88,99))
> test3 <- data.frame(x =c(22,23,24,25),y = c(55,66,77,88),z = c(99,100,111,122))
> test1
#   x  y
#1 11 11
#2 12 22
#3 13 33
#4 14 44
> test2
#      x  y
#1 33.33 88
#2 66.66 99
> test3
#   x  y   z
#1 22 55  99
#2 23 66 100
#3 24 77 111
#4 25 88 122
> bind_cols(test1,test3)
#New names:
#• `x` -> `x...1`
#• `y` -> `y...2`
#• `x` -> `x...3`
#• `y` -> `y...4`
#  x...1 y...2 x...3 y...4   z
#1    11    11    22    55  99
#2    12    22    23    66 100
#3    13    33    24    77 111
#4    14    44    25    88 122
> bind_rows(test1,test2)
#      x  y
#1 11.00 11
#2 12.00 22
#3 13.00 33
#4 14.00 44
#5 33.33 88
#6 66.66 99
> cbind(test1,test2)
#   x  y     x  y
#1 11 11 33.33 88
#2 12 22 66.66 99
#3 13 33 33.33 88
#4 14 44 66.66 99
> cbind(test1,test2,test3)
#   x  y     x  y  x  y   z
1# 11 11 33.33 88 22 55  99
#2 12 22 66.66 99 23 66 100
#3 13 33 33.33 88 24 77 111
#4 14 44 66.66 99 25 88 122

内容参考微信公众号 生信星球自己实践总结。

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

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

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

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

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1. R包安装和加载
    • 1.1 镜像设置
      • 1.2 R包安装命令
        • 1.3 dplyr包安装和加载
        • 2. dplyr基础函数
          • 2.1 新增列,mutate()
            • 2.2 按列筛选select()
              • 2.2.1 按列号筛选
              • 2.2.2 按列名筛选
            • 2.3 筛选行filter()
              • 2.5 汇总summarise()
          • 3. dplyr的两个实用技能
            • 3.1 管道操作%>% (cmd/ctl + shift +M)
              • 3.2 统计某列的unique值count()
              • 4. dplyr处理关系数据(将两个数据框连接)
                • 4.1 inner_join(内连,取交集)
                  • 4.2 left_join,左连
                    • 4.3 full.join全连
                      • 4.4 semi_join: 半连接
                        • 4.5 anti_join:返回无法和y表相匹配的x表所有记录
                          • 4.6 简单合并
                          领券
                          问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档