Day 6——野生稻花不香

第6天的學(xué)習(xí)內(nèi)容片效,簡單羅列如下:

Day 6. 學(xué)習(xí)R包.png

以dplyr為例俊嗽,學(xué)習(xí)和使用R包——多個(gè)函數(shù)的集合堕仔。

安裝和加載

# The easiest way to get dplyr is to install the whole tidyverse:
install.packages("tidyverse")

# Alternatively, install just dplyr:
install.packages("dplyr")
library(dplyr) # or require(dplyr)
test <- iris[c(1:2,51:52,101:102),] #This famous (Fisher's or Anderson's) iris data set gives the measurements in centimeters of the variables sepal length and width and petal length and width, respectively, for 50 flowers from each of 3 species of iris. The species are Iris setosa, versicolor, and virginica.

R包dplyr的基礎(chǔ)函數(shù)

1. mutate() 可以在現(xiàn)有變量的基礎(chǔ)上添加新變量
> mutate(test, new = Sepal.Length * Sepal.Width) # mutate() adds new variables that are functions of existing variables.
  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
2. select() 按列號或列名篩選變量
>select(test,1) # or select(test,Sepal.Length). # select() picks variables based on their names.
   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)) # or select(test, Petal.Length, Petal.Species) or select(test, one_of("Petal.Length", "Petal.Species"))
   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
3. filter() 按行名篩選
> filter(test, Species == "setosa") # filter() picks cases based on their values.
  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
4. summarise() 對多個(gè)數(shù)值匯總
# summarise() reduces multiple values down to a single summary.
> summarise(test, mean(Sepal.Length), sd(Sepal.Length)) # Calculate the mean and standard deviation of Sepal.Length.
  mean(Sepal.Length) sd(Sepal.Length)
1           5.916667        0.8084965
> group_by(test, Species) # Group test by 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))
# 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
5. arrange() 按某1列或某幾列對整個(gè)表格進(jìn)行排序
> arrange(test, Sepal.Length) # The default order is from small to large. # arrange() changes the ordering of the rows.
  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)) # Reverse the order using 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

R包dplyr的實(shí)用技能

1. 管道
> test %>%  # Use %>% to emphasise a sequence of actions, rather than the object that the actions are being performed on.
+   group_by(Species) %>% # %>% should always have a space before it, and should usually be followed by a new line. 
+   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. 統(tǒng)計(jì)某列中的記錄并計(jì)數(shù)
> count(test,Species)
# A tibble: 3 x 2
  Species        n
  <fct>      <int>
1 setosa         2
2 versicolor     2
3 virginica      2
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末躲因,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子趁耗,更是在濱河造成了極大的恐慌沉唠,老刑警劉巖,帶你破解...
    沈念sama閱讀 211,042評論 6 490
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件苛败,死亡現(xiàn)場離奇詭異右冻,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)著拭,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 89,996評論 2 384
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來牍帚,“玉大人儡遮,你說我怎么就攤上這事“蹈希” “怎么了鄙币?”我有些...
    開封第一講書人閱讀 156,674評論 0 345
  • 文/不壞的土叔 我叫張陵肃叶,是天一觀的道長。 經(jīng)常有香客問我十嘿,道長因惭,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 56,340評論 1 283
  • 正文 為了忘掉前任绩衷,我火速辦了婚禮蹦魔,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘咳燕。我一直安慰自己勿决,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 65,404評論 5 384
  • 文/花漫 我一把揭開白布招盲。 她就那樣靜靜地躺著低缩,像睡著了一般。 火紅的嫁衣襯著肌膚如雪曹货。 梳的紋絲不亂的頭發(fā)上咆繁,一...
    開封第一講書人閱讀 49,749評論 1 289
  • 那天,我揣著相機(jī)與錄音顶籽,去河邊找鬼玩般。 笑死,一個(gè)胖子當(dāng)著我的面吹牛蜕衡,可吹牛的內(nèi)容都是我干的壤短。 我是一名探鬼主播,決...
    沈念sama閱讀 38,902評論 3 405
  • 文/蒼蘭香墨 我猛地睜開眼慨仿,長吁一口氣:“原來是場噩夢啊……” “哼久脯!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起镰吆,我...
    開封第一講書人閱讀 37,662評論 0 266
  • 序言:老撾萬榮一對情侶失蹤帘撰,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后万皿,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體摧找,經(jīng)...
    沈念sama閱讀 44,110評論 1 303
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 36,451評論 2 325
  • 正文 我和宋清朗相戀三年牢硅,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了蹬耘。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 38,577評論 1 340
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡减余,死狀恐怖综苔,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤如筛,帶...
    沈念sama閱讀 34,258評論 4 328
  • 正文 年R本政府宣布堡牡,位于F島的核電站,受9級特大地震影響杨刨,放射性物質(zhì)發(fā)生泄漏晤柄。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 39,848評論 3 312
  • 文/蒙蒙 一妖胀、第九天 我趴在偏房一處隱蔽的房頂上張望芥颈。 院中可真熱鬧,春花似錦做粤、人聲如沸浇借。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,726評論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽妇垢。三九已至,卻和暖如春肉康,著一層夾襖步出監(jiān)牢的瞬間闯估,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 31,952評論 1 264
  • 我被黑心中介騙來泰國打工吼和, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留涨薪,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 46,271評論 2 360
  • 正文 我出身青樓炫乓,卻偏偏與公主長得像刚夺,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個(gè)殘疾皇子末捣,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 43,452評論 2 348