Day-6 Joker-ztt

学习R包:

  • 以dplyr包为例,先配置镜像,再下载安装包,最后加载包

1.先配置镜像:

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

2.安装包:

install.packages("dplyr")

或者

BiocManager::install(“dplyr”)

运算结果如下:

> install.packages("dplyr")
also installing the dependencies ‘utf8’, ‘digest’, ‘cli’, ‘crayon’, ‘fansi’, ‘lifecycle’, ‘pillar’, ‘vctrs’, ‘purrr’, ‘ellipsis’, ‘assertthat’, ‘glue’, ‘magrittr’, ‘pkgconfig’, ‘R6’, ‘Rcpp’, ‘rlang’, ‘tibble’, ‘tidyselect’, ‘BH’, ‘plogr’


  There is a binary version available but the source version is later:
     binary source needs_compilation
glue  1.3.2  1.4.0              TRUE

Do you want to install from sources the package which needs compilation? (Yes/no/cancel) yes
试开URL’https://mirrors.tuna.tsinghua.edu.cn/CRAN/bin/macosx/el-capitan/contrib/3.6/utf8_1.1.4.tgz'
Content type 'application/octet-stream' length 196648 bytes (192 KB)
==================================================
downloaded 192 KB

试开URL’https://mirrors.tuna.tsinghua.edu.cn/CRAN/bin/macosx/el-capitan/contrib/3.6/digest_0.6.25.tgz'
Content type 'application/octet-stream' length 246357 bytes (240 KB)
==================================================
downloaded 240 KB

试开URL’https://mirrors.tuna.tsinghua.edu.cn/CRAN/bin/macosx/el-capitan/contrib/3.6/cli_2.0.2.tgz'
Content type 'application/octet-stream' length 395137 bytes (385 KB)
==================================================
downloaded 385 KB

试开URL’https://mirrors.tuna.tsinghua.edu.cn/CRAN/bin/macosx/el-capitan/contrib/3.6/crayon_1.3.4.tgz'
Content type 'application/octet-stream' length 749917 bytes (732 KB)
==================================================
downloaded 732 KB

试开URL’https://mirrors.tuna.tsinghua.edu.cn/CRAN/bin/macosx/el-capitan/contrib/3.6/fansi_0.4.1.tgz'
Content type 'application/octet-stream' length 210779 bytes (205 KB)
==================================================
downloaded 205 KB

试开URL’https://mirrors.tuna.tsinghua.edu.cn/CRAN/bin/macosx/el-capitan/contrib/3.6/lifecycle_0.2.0.tgz'
Content type 'application/octet-stream' length 91621 bytes (89 KB)
==================================================
downloaded 89 KB

试开URL’https://mirrors.tuna.tsinghua.edu.cn/CRAN/bin/macosx/el-capitan/contrib/3.6/pillar_1.4.3.tgz'
Content type 'application/octet-stream' length 178277 bytes (174 KB)
==================================================
downloaded 174 KB

试开URL’https://mirrors.tuna.tsinghua.edu.cn/CRAN/bin/macosx/el-capitan/contrib/3.6/vctrs_0.2.4.tgz'
Content type 'application/octet-stream' length 1077741 bytes (1.0 MB)
==================================================
downloaded 1.0 MB

试开URL’https://mirrors.tuna.tsinghua.edu.cn/CRAN/bin/macosx/el-capitan/contrib/3.6/purrr_0.3.3.tgz'
Content type 'application/octet-stream' length 412501 bytes (402 KB)
==================================================
downloaded 402 KB

试开URL’https://mirrors.tuna.tsinghua.edu.cn/CRAN/bin/macosx/el-capitan/contrib/3.6/ellipsis_0.3.0.tgz'
Content type 'application/octet-stream' length 33047 bytes (32 KB)
==================================================
downloaded 32 KB

试开URL’https://mirrors.tuna.tsinghua.edu.cn/CRAN/bin/macosx/el-capitan/contrib/3.6/assertthat_0.2.1.tgz'
Content type 'application/octet-stream' length 53625 bytes (52 KB)
==================================================
downloaded 52 KB

试开URL’https://mirrors.tuna.tsinghua.edu.cn/CRAN/bin/macosx/el-capitan/contrib/3.6/magrittr_1.5.tgz'
Content type 'application/octet-stream' length 154842 bytes (151 KB)
==================================================
downloaded 151 KB

试开URL’https://mirrors.tuna.tsinghua.edu.cn/CRAN/bin/macosx/el-capitan/contrib/3.6/pkgconfig_2.0.3.tgz'
Content type 'application/octet-stream' length 17573 bytes (17 KB)
==================================================
downloaded 17 KB

试开URL’https://mirrors.tuna.tsinghua.edu.cn/CRAN/bin/macosx/el-capitan/contrib/3.6/R6_2.4.1.tgz'
Content type 'application/octet-stream' length 57479 bytes (56 KB)
==================================================
downloaded 56 KB

试开URL’https://mirrors.tuna.tsinghua.edu.cn/CRAN/bin/macosx/el-capitan/contrib/3.6/Rcpp_1.0.4.tgz'
Content type 'application/octet-stream' length 3124401 bytes (3.0 MB)
==================================================
downloaded 3.0 MB

试开URL’https://mirrors.tuna.tsinghua.edu.cn/CRAN/bin/macosx/el-capitan/contrib/3.6/rlang_0.4.5.tgz'
Content type 'application/octet-stream' length 1182651 bytes (1.1 MB)
==================================================
downloaded 1.1 MB

试开URL’https://mirrors.tuna.tsinghua.edu.cn/CRAN/bin/macosx/el-capitan/contrib/3.6/tibble_3.0.0.tgz'
Content type 'application/octet-stream' length 384238 bytes (375 KB)
==================================================
downloaded 375 KB

试开URL’https://mirrors.tuna.tsinghua.edu.cn/CRAN/bin/macosx/el-capitan/contrib/3.6/tidyselect_1.0.0.tgz'
Content type 'application/octet-stream' length 240162 bytes (234 KB)
==================================================
downloaded 234 KB

试开URL’https://mirrors.tuna.tsinghua.edu.cn/CRAN/bin/macosx/el-capitan/contrib/3.6/BH_1.72.0-3.tgz'
Content type 'application/octet-stream' length 11253901 bytes (10.7 MB)
==================================================
downloaded 10.7 MB

试开URL’https://mirrors.tuna.tsinghua.edu.cn/CRAN/bin/macosx/el-capitan/contrib/3.6/plogr_0.2.0.tgz'
Content type 'application/octet-stream' length 13178 bytes (12 KB)
==================================================
downloaded 12 KB

试开URL’https://mirrors.tuna.tsinghua.edu.cn/CRAN/bin/macosx/el-capitan/contrib/3.6/dplyr_0.8.5.tgz'
Content type 'application/octet-stream' length 6859111 bytes (6.5 MB)
==================================================
downloaded 6.5 MB


The downloaded binary packages are in
    /var/folders/7c/mpkw17q950jfp2gj5w1hc3vr0000gn/T//Rtmpk6zLHd/downloaded_packages
installing the source package ‘glue’

试开URL’https://mirrors.tuna.tsinghua.edu.cn/CRAN/src/contrib/glue_1.4.0.tar.gz'
Content type 'application/x-gzip' length 98619 bytes (96 KB)
==================================================
downloaded 96 KB

* installing *source* package ‘glue’ ...
** 成功将‘glue’程序包解包并MD5和检查
** using staged installation
** libs
clang -I"/Library/Frameworks/R.framework/Resources/include" -DNDEBUG   -isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk -I/usr/local/include  -fPIC  -Wall -g -O2  -c glue.c -o glue.o
clang -I"/Library/Frameworks/R.framework/Resources/include" -DNDEBUG   -isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk -I/usr/local/include  -fPIC  -Wall -g -O2  -c init.c -o init.o
clang -I"/Library/Frameworks/R.framework/Resources/include" -DNDEBUG   -isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk -I/usr/local/include  -fPIC  -Wall -g -O2  -c trim.c -o trim.o
clang -dynamiclib -Wl,-headerpad_max_install_names -undefined dynamic_lookup -single_module -multiply_defined suppress -L/Library/Frameworks/R.framework/Resources/lib -L/usr/local/lib -o glue.so glue.o init.o trim.o -F/Library/Frameworks/R.framework/.. -framework R -Wl,-framework -Wl,CoreFoundation
installing to /Library/Frameworks/R.framework/Versions/3.6/Resources/library/00LOCK-glue/00new/glue/libs
** R
** inst
** byte-compile and prepare package for lazy loading
** help
*** installing help indices
*** copying figures
** building package indices
** installing vignettes
** testing if installed package can be loaded from temporary location
** checking absolute paths in shared objects and dynamic libraries
** testing if installed package can be loaded from final location
** testing if installed package keeps a record of temporary installation path
* DONE (glue)

The downloaded source packages are in
    ‘/private/var/folders/7c/mpkw17q950jfp2gj5w1hc3vr0000gn/T/Rtmpk6zLHd/downloaded_packages’

3.加载包

library("dplyr")

或者

require("dplyr")

运算结果如下:

> library("dplyr")

载入程辑包:‘dplyr’

The following objects are masked from ‘package:stats’:

    filter, lag

The following objects are masked from ‘package:base’:

    intersect, setdiff, setequal, union
  • 示例数据直接使用内置数据集iris的简化版:

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

dplyr五个基础函数

1.mutate(), 新增

> 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
  1. select() ,按 筛选
    (1)按列号筛选
> select(test,1) #选取第一列
    Sepal.Length
1            5.1
2            4.9
51           7.0
52           6.4
101          6.3
102          5.8
> select(test,c(1,5)) #选取第一列和第五列
    Sepal.Length    Species
1            5.1     setosa
2            4.9     setosa
51           7.0 versicolor
52           6.4 versicolor
101          6.3  virginica
102          5.8  virginica

(2)按列名筛选

> select(test, Petal.Length, Petal.Width)
   Petal.Length Petal.Width
1            1.4         0.2
2            1.4         0.2
51           4.7         1.4
52           4.5         1.5
101          6.0         2.5
102          5.1         1.9
> vars <- c("Petal.Length", "Petal.Width")
> select(test, one_of(vars))
    Petal.Length Petal.Width
1            1.4         0.2
2            1.4         0.2
51           4.7         1.4
52           4.5         1.5
101          6.0         2.5
102          5.1         1.9
  1. filter(), 筛选
> filter(test, Species == "setosa")
  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
> filter(test, Species == "setosa"&Sepal.Length > 5 )
  Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1          5.1         3.5          1.4         0.2  setosa
> filter(test, Species %in% c("setosa","versicolor"))
  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
  1. arrange(), 按 某1列某几列 对整个表格进行排序
> 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
> arrange(test, desc(Sepal.Length)) #desc表示从大到小
  Sepal.Length Sepal.Width Petal.Length Petal.Width    Species
1          7.0         3.2          4.7         1.4 versicolor
2          6.4         3.2          4.5         1.5 versicolor
3          6.3         3.3          6.0         2.5  virginica
4          5.8         2.7          5.1         1.9  virginica
5          5.1         3.5          1.4         0.2     setosa
6          4.9         3.0          1.4         0.2     setosa
  1. summarise():汇总

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

> summarise(test, mean(Sepal.Length), sd(Sepal.Length)) # 计算Sepal.Length的平均值和标准差
  mean(Sepal.Length) sd(Sepal.Length)
1           5.916667        0.8084965
> group_by(test, Species)  #按照Species分组
# A tibble: 6 x 5
# Groups:   Species [3]
  Sepal.Length Sepal.Width Petal.Length Petal.Width Species   
*        <dbl>       <dbl>        <dbl>       <dbl> <fct>     
1          5.1         3.5          1.4         0.2 setosa    
2          4.9         3            1.4         0.2 setosa    
3          7           3.2          4.7         1.4 versicolor
4          6.4         3.2          4.5         1.5 versicolor
5          6.3         3.3          6           2.5 virginica 
6          5.8         2.7          5.1         1.9 virginica 
> summarise(group_by(test, Species),mean(Sepal.Length), sd(Sepal.Length)) #先按照Species分为3组,然后计算每组Sepal.Length的平均值和标准差
# A tibble: 3 x 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

dplyr两个实用技能

1.管道操作 %>% (即用键盘 cmd/ctr + shift + M 可打出 %>% )

加载任意一个tidyverse包即可用管道符号

> test %>% 
+ group_by(Species) %>% 
+ summarise(mean(Sepal.Length), sd(Sepal.Length))
# A tibble: 3 x 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

2.count统计某列的unique值

> count(test,Species)
# A tibble: 3 x 2
  Species        n
  <fct>      <int>
1 setosa         2
2 versicolor     2
3 virginica      2

dplyr处理关系数据

即将2个表进行连接,注意:不要引入factor

> options(stringsAsFactors = F)
> test1 <- data.frame(x = c('b','e','f','x'),z = c("A","B","C",'D'),stringsAsFactors = F)
> test1
  x z
1 b A
2 e B
3 f C
4 x D
> test2 <- data.frame(x = c('a','b','c','d','e','f'),y = c(1,2,3,4,5,6),stringsAsFactors = F)
> test2
  x y
1 a 1
2 b 2
3 c 3
4 d 4
5 e 5
6 f 6

1.內连 inner_join ,取 交集

> inner_join(test1, test2, by = "x") #通过两表x相同的部分,连接两表
  x z y
1 b A 2
2 e B 5
3 f C 6

2.左连 left_join

> left_join(test1, test2, by = 'x') #可以看到以NA来表示不存在的元素
  x z  y
1 b A  2
2 e B  5
3 f C  6
4 x D NA
> left_join(test2, test1, by = 'x')
  x y    z
1 a 1 <NA>
2 b 2    A
3 c 3 <NA>
4 d 4 <NA>
5 e 5    B
6 f 6    C

3.全连 full_join

> full_join( test1, test2, by = 'x')
  x    z  y
1 b    A  2
2 e    B  5
3 f    C  6
4 x    D NA
5 a <NA>  1
6 c <NA>  3
7 d <NA>  4

4.半连接:返回能够与y表匹配的x表所有记录 semi_join

> semi_join(x = test1, y = test2, by = 'x')
  x z
1 b A
2 e B
3 f C

5.反连接:返回无法与y表匹配的x表的所记录 anti_join

> anti_join(x = test2, y = test1, by = 'x') #注意,y来源于test1
  x y
1 a 1
2 c 3
3 d 4

6.简单合并

相当于base包里的 cbind() 函数和 rbind() 函数; 注意,bind_rows() 函数需要两个表格 列数 相同,而 bind_cols() 函数则需要两个数据框有相同的 行数

> test1 <- data.frame(x = c(1,2,3,4), y = c(10,20,30,40))
> test1 
  x  y
1 1 10
2 2 20
3 3 30
4 4 40
> test2 <- data.frame(x = c(5,6), y = c(50,60))
> test2
  x  y
1 5 50
2 6 60
> test3 <- data.frame(z = c(100,200,300,400))
> test3
    z
1 100
2 200
3 300
4 400
 bind_rows(test1, test2) #bind_rows()函数需要两个表格 列数 相同
  x  y
1 1 10
2 2 20
3 3 30
4 4 40
5 5 50
6 6 60
> bind_cols(test1, test3) #bind_cols()函数则需要两个数据框有相同的 行数
  x  y   z
1 1 10 100
2 2 20 200
3 3 30 300
4 4 40 400

结语:读懂函数的意义就行

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 204,530评论 6 478
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 86,403评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 151,120评论 0 337
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,770评论 1 277
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,758评论 5 367
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,649评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,021评论 3 398
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,675评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,931评论 1 299
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,659评论 2 321
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,751评论 1 330
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,410评论 4 321
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,004评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,969评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,203评论 1 260
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 45,042评论 2 350
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,493评论 2 343

推荐阅读更多精彩内容