The data set concerns species and weight of animals caught in plots in a study area in Arizona over time.

Each row holds information for a single animal, and the columns represent:

打開pacman的tidyverse套件

pacman::p_load(tidyverse)

將dta設定為讀"http://kbroman.org/datacarp/portal_data_joined.csv"這個csv檔

dta <- read_csv("http://kbroman.org/datacarp/portal_data_joined.csv")
## Parsed with column specification:
## cols(
##   record_id = col_double(),
##   month = col_double(),
##   day = col_double(),
##   year = col_double(),
##   plot_id = col_double(),
##   species_id = col_character(),
##   sex = col_character(),
##   hindfoot_length = col_double(),
##   weight = col_double(),
##   genus = col_character(),
##   species = col_character(),
##   taxa = col_character(),
##   plot_type = col_character()
## )

看這個檔的數據資料,glimpse會想把全部資料呈現出來

glimpse(dta)
## Observations: 34,786
## Variables: 13
## $ record_id       <dbl> 1, 72, 224, 266, 349, 363, 435, 506, 588, 661, 748,...
## $ month           <dbl> 7, 8, 9, 10, 11, 11, 12, 1, 2, 3, 4, 5, 6, 8, 9, 10...
## $ day             <dbl> 16, 19, 13, 16, 12, 12, 10, 8, 18, 11, 8, 6, 9, 5, ...
## $ year            <dbl> 1977, 1977, 1977, 1977, 1977, 1977, 1977, 1978, 197...
## $ plot_id         <dbl> 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, ...
## $ species_id      <chr> "NL", "NL", "NL", "NL", "NL", "NL", "NL", "NL", "NL...
## $ sex             <chr> "M", "M", NA, NA, NA, NA, NA, NA, "M", NA, NA, "M",...
## $ hindfoot_length <dbl> 32, 31, NA, NA, NA, NA, NA, NA, NA, NA, NA, 32, NA,...
## $ weight          <dbl> NA, NA, NA, NA, NA, NA, NA, NA, 218, NA, NA, 204, 2...
## $ genus           <chr> "Neotoma", "Neotoma", "Neotoma", "Neotoma", "Neotom...
## $ species         <chr> "albigula", "albigula", "albigula", "albigula", "al...
## $ taxa            <chr> "Rodent", "Rodent", "Rodent", "Rodent", "Rodent", "...
## $ plot_type       <chr> "Control", "Control", "Control", "Control", "Contro...

看這個檔案的的列與行多長

dim(dta)
## [1] 34786    13

select是用來選要分析的欄位,欄位子集 (Column)

dplyr::select(dta, plot_id, species_id, weight) %>% head()
dplyr::select(dta, -record_id, -species_id) %>% head()

filter()是用來選要分析的觀察值,觀察值子集 (Row)

dplyr::filter(dta, year == 1995) %>% head()

將species, sex, weight取出來另外製表,其中選擇weight小於3等於5的

head(dplyr::select(dplyr::filter(dta, weight <= 5), species_id, sex, weight))

將species, sex, weight取出來另外製表,其中選擇weight小於3等於5的另一種寫法

dta %>% 
  dplyr::filter(weight <= 5) %>% 
  dplyr::select(species_id, sex, weight) %>% 
  head

將weight換單位之後另外製表

dta %>% 
  mutate(weight_kg = weight / 1000,
         weight_lb = weight_kg * 2.2) %>% 
  head()

先選觀察值,將weight的NA設定遺漏值,然後用sex與species來做根據,使用weight的平均值,將weight的平均值從大到小排列

dta %>% 
  filter(!is.na(weight)) %>%
  group_by(sex, species_id) %>%
  summarize(mean_weight = mean(weight)) %>%
  arrange(desc(mean_weight)) %>% 
  head()

製作總數表

dta %>%
  group_by(sex) %>%
  tally

算性別總數

dta %>%
  count(sex)

也是算性別總數

dta %>%
  group_by(sex) %>%
  summarize(count = n())

再算性別總數,然後設定year的遺漏值

dta %>%
  group_by(sex) %>%
  summarize(count = sum(!is.na(year)))

將weight的觀察值設遺漏值,用genus與plot_id做根據,以weight的平均數製表

dta_gw <- dta %>% 
  filter(!is.na(weight)) %>%
  group_by(genus, plot_id) %>%
  summarize(mean_weight = mean(weight))

看以上製的表

glimpse(dta_gw)
## Observations: 196
## Variables: 3
## Groups: genus [10]
## $ genus       <chr> "Baiomys", "Baiomys", "Baiomys", "Baiomys", "Baiomys", ...
## $ plot_id     <dbl> 1, 2, 3, 5, 18, 19, 20, 21, 1, 2, 3, 4, 5, 6, 7, 8, 9, ...
## $ mean_weight <dbl> 7.000000, 6.000000, 8.611111, 7.750000, 9.500000, 9.533...

將以上製的表其中的genus下層的變數伸展開來

dta_w <- dta_gw %>%
  spread(key = genus, value = mean_weight)

看以上製的表

glimpse(dta_w)
## Observations: 24
## Variables: 11
## $ plot_id         <dbl> 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, ...
## $ Baiomys         <dbl> 7.000000, 6.000000, 8.611111, NA, 7.750000, NA, NA,...
## $ Chaetodipus     <dbl> 22.19939, 25.11014, 24.63636, 23.02381, 17.98276, 2...
## $ Dipodomys       <dbl> 60.23214, 55.68259, 52.04688, 57.52454, 51.11356, 5...
## $ Neotoma         <dbl> 156.2222, 169.1436, 158.2414, 164.1667, 190.0370, 1...
## $ Onychomys       <dbl> 27.67550, 26.87302, 26.03241, 28.09375, 27.01695, 2...
## $ Perognathus     <dbl> 9.625000, 6.947368, 7.507812, 7.824427, 8.658537, 7...
## $ Peromyscus      <dbl> 22.22222, 22.26966, 21.37037, 22.60000, 21.23171, 2...
## $ Reithrodontomys <dbl> 11.375000, 10.680556, 10.516588, 10.263158, 11.1545...
## $ Sigmodon        <dbl> NA, 70.85714, 65.61404, 82.00000, 82.66667, 68.7777...
## $ Spermophilus    <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,...

將genus, mean_weight下層變數延展開來製表,遺漏值使用0

dta_gw %>%
  spread(genus, mean_weight, fill = 0) %>%
  head()

使genus的變數收攏起來並對照mean_weight、-plot_id

dta_l <- dta_w %>%
  gather(key = genus, value = mean_weight, -plot_id)

看以上製的表

glimpse(dta_l)
## Observations: 240
## Variables: 3
## $ plot_id     <dbl> 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, ...
## $ genus       <chr> "Baiomys", "Baiomys", "Baiomys", "Baiomys", "Baiomys", ...
## $ mean_weight <dbl> 7.000000, 6.000000, 8.611111, NA, 7.750000, NA, NA, NA,...

將上面那個表轉直行

dta_w %>%
  gather(key = genus, value = mean_weight, Baiomys:Spermophilus) %>%
  head()

將檔案的weight、hindfoot_length、sex的觀察值設定遺漏值{r} dta_complete <- dta %>% filter(!is.na(weight), !is.na(hindfoot_length), !is.na(sex))

`之後再把species_id不是0,數據大於等於50的觀察值等資料單獨調出來

`{r} species_counts <- dta_complete %>% count(species_id) %>% filter(n >= 50)



`
再將species_id在species_counts$species_id5中的觀察值單獨調出來

`{r}
dta_complete <- dta_complete %>%
  filter(species_id %in% species_counts$species_id)