除了对于数据的选择和筛选,我们还要对数据进行汇总和简单的分析。这个时候我们就需要用到一系列的函数,这里用到最多的还是summarise()函数。这个是基于Summarizing
and slicing your
data的学习笔记
library(tidyverse)
data("msleep")
简单的统计
某一个变量的计数
我们可以通过count()函数来对某一列或者某几列来进行统计。这个函数里面,输入多少个列就是对多少个列合并统计。同时可以通过sort
参数来指定说是否对其进行排序。
msleep %>%
count(order, vore, sort = TRUE)
## # A tibble: 32 x 3
## order vore n
## <chr> <chr> <int>
## 1 Rodentia herbi 16
## 2 Carnivora carni 12
## 3 Primates omni 10
## 4 Artiodactyla herbi 5
## 5 Cetacea carni 3
## 6 Perissodactyla herbi 3
## 7 Rodentia <NA> 3
## 8 Soricomorpha omni 3
## 9 Chiroptera insecti 2
## 10 Hyracoidea herbi 2
## # … with 22 more rows
添加观察的总数
我们可以通过tally()来计算行数。这个函数和nrow()其实是一样的。
msleep %>%
tally()
## # A tibble: 1 x 1
## n
## <int>
## 1 83
另外,我们可以通过add_tally()来数据添加一列,列的内容是这个数据的行数
msleep %>%
select(1:3) %>%
add_tally() %>% head
## # A tibble: 6 x 4
## name genus vore n
## <chr> <chr> <chr> <int>
## 1 Cheetah Acinonyx carni 83
## 2 Owl monkey Aotus omni 83
## 3 Mountain beaver Aplodontia herbi 83
## 4 Greater short-tailed shrew Blarina omni 83
## 5 Cow Bos herbi 83
## 6 Three-toed sloth Bradypus herbi 83
更进一步的,我们可以通过add_count()当中指定一个变量。来在数据框当中添加一列,列的内容是某一列的个数统计量。其实这个函数就是mutate(n = count(XX))
msleep %>%
select(name:vore) %>%
add_count(vore) %>% head
## # A tibble: 6 x 4
## name genus vore n
## <chr> <chr> <chr> <int>
## 1 Cheetah Acinonyx carni 19
## 2 Owl monkey Aotus omni 20
## 3 Mountain beaver Aplodontia herbi 32
## 4 Greater short-tailed shrew Blarina omni 20
## 5 Cow Bos herbi 32
## 6 Three-toed sloth Bradypus herbi 32
summarise的使用
对于简单的计数,由于是常用的所以就额外设置了函数。但是很多时候的一些统计量,是需要自己来指定的。这个时候就用到了summarise()函数了。这个函数可以让我们对基于某一列的数据来进行统计。同样的,可以通过group_by()可以让我们在统计的时候基于某一个分组来进行统计
msleep %>%
group_by(vore) %>%
summarise(n = n(), average = mean(sleep_total), maximum = max(sleep_total))
## # A tibble: 5 x 4
## vore n average maximum
## <chr> <int> <dbl> <dbl>
## 1 carni 19 10.4 19.4
## 2 herbi 32 9.51 16.6
## 3 insecti 5 14.9 19.9
## 4 omni 20 10.9 18
## 5 <NA> 7 10.2 13.7
对多列进行批量统计
同样的summarise()函数存在批量统计的扩展函数。这个函数就是summarise_all/if/at。
具体的用法,可以参见之前的帖子。这里就不做详细的介绍了。
msleep %>%
group_by(vore) %>%
summarise_all(~mean(., na.rm = TRUE) + 5)
## # A tibble: 5 x 11
## vore name genus order conservation sleep_total sleep_rem sleep_cycle awake
## <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 carni NA NA NA NA 15.4 7.29 5.37 18.6
## 2 herbi NA NA NA NA 14.5 6.37 5.42 19.5
## 3 inse… NA NA NA NA 19.9 8.52 5.16 14.1
## 4 omni NA NA NA NA 15.9 6.96 5.59 18.1
## 5 <NA> NA NA NA NA 15.2 6.88 5.18 18.8
## # … with 2 more variables: brainwt <dbl>, bodywt <dbl>
msleep %>%
group_by(vore) %>%
summarise_if(is.numeric, mean, na.rm=TRUE)
## # A tibble: 5 x 7
## vore sleep_total sleep_rem sleep_cycle awake brainwt bodywt
## <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 carni 10.4 2.29 0.373 13.6 0.0793 90.8
## 2 herbi 9.51 1.37 0.418 14.5 0.622 367.
## 3 insecti 14.9 3.52 0.161 9.06 0.0216 12.9
## 4 omni 10.9 1.96 0.592 13.1 0.146 12.7
## 5 <NA> 10.2 1.88 0.183 13.8 0.00763 0.858
msleep %>%
group_by(vore) %>%
summarise_at(vars(contains("sleep")), mean, na.rm=TRUE) %>%
rename_at(vars(contains("sleep")), ~paste0("avg_", .))
## # A tibble: 5 x 4
## vore avg_sleep_total avg_sleep_rem avg_sleep_cycle
## <chr> <dbl> <dbl> <dbl>
## 1 carni 10.4 2.29 0.373
## 2 herbi 9.51 1.37 0.418
## 3 insecti 14.9 3.52 0.161
## 4 omni 10.9 1.96 0.592
## 5 <NA> 10.2 1.88 0.183
网友评论