Day-6 Joker-ztt

學(xué)習(xí)R包:

  • 以dplyr包為例姐呐,先配置鏡像,再下載安裝包徒欣,最后加載包

1.先配置鏡像:

options("repos" = c(CRAN="https://mirrors.tuna.tsinghua.edu.cn/CRAN/")) #對應(yīng)清華源
options(BioC_mirror="https://mirrors.ustc.edu.cn/bioc/") #對應(yīng)中科大源

2.安裝包:

install.packages("dplyr")

或者

BiocManager::install(“dplyr”)

運(yùn)算結(jié)果如下:

> install.packages("dplyr")
also installing the dependencies ‘utf8’, ‘digest’, ‘cli’, ‘crayon’, ‘fansi’, ‘lifecycle’, ‘pillar’, ‘vctrs’, ‘purrr’, ‘ellipsis’, ‘a(chǎn)ssertthat’, ‘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")

運(yùn)算結(jié)果如下:

> 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
  • 示例數(shù)據(jù)直接使用內(nèi)置數(shù)據(jù)集iris的簡化版:

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

dplyr五個基礎(chǔ)函數(shù)

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列某幾列 對整個表格進(jìn)行排序
> arrange(test, Sepal.Length) #默認(rèn)從小到大
  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():匯總

對數(shù)據(jù)進(jìn)行匯總操作,結(jié)合group_by使用實(shí)用性強(qiáng)

> summarise(test, mean(Sepal.Length), sd(Sepal.Length)) # 計算Sepal.Length的平均值和標(biāo)準(zhǔn)差
  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的平均值和標(biāo)準(zhǔn)差
# 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兩個實(shí)用技能

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統(tǒng)計某列的unique值

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

dplyr處理關(guān)系數(shù)據(jù)

即將2個表進(jìn)行連接,注意:不要引入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.內(nèi)連 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.反連接:返回?zé)o法與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.簡單合并

相當(dāng)于base包里的 cbind() 函數(shù)和 rbind() 函數(shù); 注意,bind_rows() 函數(shù)需要兩個表格 列數(shù) 相同葵礼,而 bind_cols() 函數(shù)則需要兩個數(shù)據(jù)框有相同的 行數(shù)

> 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()函數(shù)需要兩個表格 列數(shù) 相同
  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()函數(shù)則需要兩個數(shù)據(jù)框有相同的 行數(shù)
  x  y   z
1 1 10 100
2 2 20 200
3 3 30 300
4 4 40 400

結(jié)語:讀懂函數(shù)的意義就行

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末号阿,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子鸳粉,更是在濱河造成了極大的恐慌扔涧,老刑警劉巖,帶你破解...
    沈念sama閱讀 218,386評論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件届谈,死亡現(xiàn)場離奇詭異枯夜,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)艰山,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,142評論 3 394
  • 文/潘曉璐 我一進(jìn)店門湖雹,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人程剥,你說我怎么就攤上這事劝枣。” “怎么了织鲸?”我有些...
    開封第一講書人閱讀 164,704評論 0 353
  • 文/不壞的土叔 我叫張陵舔腾,是天一觀的道長。 經(jīng)常有香客問我搂擦,道長稳诚,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,702評論 1 294
  • 正文 為了忘掉前任瀑踢,我火速辦了婚禮扳还,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘橱夭。我一直安慰自己氨距,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,716評論 6 392
  • 文/花漫 我一把揭開白布棘劣。 她就那樣靜靜地躺著俏让,像睡著了一般。 火紅的嫁衣襯著肌膚如雪茬暇。 梳的紋絲不亂的頭發(fā)上首昔,一...
    開封第一講書人閱讀 51,573評論 1 305
  • 那天,我揣著相機(jī)與錄音糙俗,去河邊找鬼勒奇。 笑死,一個胖子當(dāng)著我的面吹牛巧骚,可吹牛的內(nèi)容都是我干的赊颠。 我是一名探鬼主播格二,決...
    沈念sama閱讀 40,314評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼巨税!你這毒婦竟也來了蟋定?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,230評論 0 276
  • 序言:老撾萬榮一對情侶失蹤草添,失蹤者是張志新(化名)和其女友劉穎驶兜,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體远寸,經(jīng)...
    沈念sama閱讀 45,680評論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡抄淑,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,873評論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了驰后。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片肆资。...
    茶點(diǎn)故事閱讀 39,991評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖灶芝,靈堂內(nèi)的尸體忽然破棺而出郑原,到底是詐尸還是另有隱情,我是刑警寧澤夜涕,帶...
    沈念sama閱讀 35,706評論 5 346
  • 正文 年R本政府宣布犯犁,位于F島的核電站,受9級特大地震影響女器,放射性物質(zhì)發(fā)生泄漏酸役。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,329評論 3 330
  • 文/蒙蒙 一驾胆、第九天 我趴在偏房一處隱蔽的房頂上張望涣澡。 院中可真熱鬧,春花似錦丧诺、人聲如沸入桂。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,910評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽事格。三九已至,卻和暖如春搞隐,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背远搪。 一陣腳步聲響...
    開封第一講書人閱讀 33,038評論 1 270
  • 我被黑心中介騙來泰國打工劣纲, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人谁鳍。 一個月前我還...
    沈念sama閱讀 48,158評論 3 370
  • 正文 我出身青樓癞季,卻偏偏與公主長得像劫瞳,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子绷柒,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,941評論 2 355

推薦閱讀更多精彩內(nèi)容