美文网首页统计分析
用dplyr包进行数据清理-mutate()和arrange()

用dplyr包进行数据清理-mutate()和arrange()

作者: 新云旧雨 | 来源:发表于2019-11-04 16:53 被阅读0次

笔记说明

dplyr包是一个用于数据清理的高效r包,也是tidyverse的核心包之一。
dplyr包的常用操作包括:
mutate() adds new variables that are functions of existing variables
select()picks variables based on their names.
filter() picks cases based on their values.
summarise() reduces multiple values down to a single summary.
arrange() changes the ordering of the rows.
group_by()allows you to perform any operation “by group”

之前的笔记(用dplyr包进行数据清理-filter()和select()用dplyr包进行数据清理-group_by()和summarise())中介绍了filter()select()group_by()summarise(),本次笔记介绍mutate()arrange()

主要参考:https://b-rodrigues.github.io/modern_R/descriptive-statistics-and-data-manipulation.html#the-tidyverses-enfant-prodige-dplyr
推荐阅读:https://dplyr.tidyverse.org/

准备工作

加载dplyr包

library(dplyr)

数据准备,我们使用plm包中的Gasoline数据集作为示例数据。该数据集包含1960至1978年间18个国家的汽油消耗量。原始数据是一个data.frame对象,我们用as_tibble()将其转换为一个tibble对象。
可以把tibble理解成一个优化版的data.frame。dplyr包中的各个函数可以作用于data.frame对象,也可以作用于tibble对象。

# 数据准备
install.packages("plm")
data(Gasoline, package = "plm")
gasoline <- as_tibble(Gasoline)

用mutate()函数生成新变量

mutate()在原数据的基础上新增变量,其用法是summarise(data, name=value...)其中data是要操作的数据集,name是结果中显示的变量名,value是表达式。多个name-value之间用逗号分隔。新变量名若与已有变量重名则会覆盖重名的已有变量,value为NULL则会删除对应变量。

gasoline %>%
  group_by(country) %>%
  mutate(spam = exp(lgaspcar + lincomep))
## # A tibble: 342 x 7
## # Groups:   country [18]
##    country  year lgaspcar lincomep  lrpmg lcarpcap   spam
##    <fct>   <int>    <dbl>    <dbl>  <dbl>    <dbl>  <dbl>
##  1 AUSTRIA  1960     4.17    -6.47 -0.335    -9.77 0.100 
##  2 AUSTRIA  1961     4.10    -6.43 -0.351    -9.61 0.0978
##  3 AUSTRIA  1962     4.07    -6.41 -0.380    -9.46 0.0969
##  4 AUSTRIA  1963     4.06    -6.37 -0.414    -9.34 0.0991
##  5 AUSTRIA  1964     4.04    -6.32 -0.445    -9.24 0.102 
##  6 AUSTRIA  1965     4.03    -6.29 -0.497    -9.12 0.104 
##  7 AUSTRIA  1966     4.05    -6.25 -0.467    -9.02 0.110 
##  8 AUSTRIA  1967     4.05    -6.23 -0.506    -8.93 0.113 
##  9 AUSTRIA  1968     4.05    -6.21 -0.522    -8.85 0.115 
## 10 AUSTRIA  1969     4.05    -6.15 -0.559    -8.79 0.122 
## # … with 332 more rows

transmute()mutate()一样可以生成新变量,用法相同,不同之处在于transmute()返回的结果中只会包括新生成的变量而没有原有变量。

gasoline %>%
  group_by(country) %>%
  transmute(spam = exp(lgaspcar + lincomep))
## # A tibble: 342 x 2
## # Groups:   country [18]
##    country   spam
##    <fct>    <dbl>
##  1 AUSTRIA 0.100 
##  2 AUSTRIA 0.0978
##  3 AUSTRIA 0.0969
##  4 AUSTRIA 0.0991
##  5 AUSTRIA 0.102 
##  6 AUSTRIA 0.104 
##  7 AUSTRIA 0.110 
##  8 AUSTRIA 0.113 
##  9 AUSTRIA 0.115 
## 10 AUSTRIA 0.122 
## # … with 332 more rows

有许多有用的函数可以帮助生成新变量:

  • log()等数学运算函数
  • lead()lag() 取之前一个(或往前第几个)观测的某变量值或之后一个(或往后第几个)观测的某变量值
  • if_else(),recode(),case_when()

这里介绍一下case_when()的用法,还有很多其他函数可以参见:https://dplyr.tidyverse.org/reference/mutate.html
case_when()应用举例:

gasoline %>%
  mutate(
    year_cat = case_when(
      year < median(year)  ~ "small",
      year == median(year) ~ "median",
      year > median(year)  ~ "large"
    )
  )
# A tibble: 342 x 7
   country  year lgaspcar lincomep  lrpmg lcarpcap year_cat
   <fct>   <int>    <dbl>    <dbl>  <dbl>    <dbl> <chr>   
 1 AUSTRIA  1960     4.17    -6.47 -0.335    -9.77 small   
 2 AUSTRIA  1961     4.10    -6.43 -0.351    -9.61 small   
 3 AUSTRIA  1962     4.07    -6.41 -0.380    -9.46 small   
 4 AUSTRIA  1963     4.06    -6.37 -0.414    -9.34 small   
 5 AUSTRIA  1964     4.04    -6.32 -0.445    -9.24 small   
 6 AUSTRIA  1965     4.03    -6.29 -0.497    -9.12 small   
 7 AUSTRIA  1966     4.05    -6.25 -0.467    -9.02 small   
 8 AUSTRIA  1967     4.05    -6.23 -0.506    -8.93 small   
 9 AUSTRIA  1968     4.05    -6.21 -0.522    -8.85 small   
10 AUSTRIA  1969     4.05    -6.15 -0.559    -8.79 median  
# ... with 332 more rows

上例中利用case_when()根据year变量的不同取值情况生成了year_cat变量。
case_when()中的内容由一系列由~分隔的公式组成,~的左边的表达式成立时则返回对应~右边的内容,公式之间用逗号分隔。

用arrange()函数对数据进行排序

用法:arrange(.data, ..., .by_group = FALSE)
···中列出需要按照排序的变量名,用逗号分隔。

gasoline %>%
  arrange(lgaspcar)
## # A tibble: 342 x 6
##    country  year lgaspcar lincomep   lrpmg lcarpcap
##    <chr>   <dbl>    <dbl>    <dbl>   <dbl>    <dbl>
##  1 italy    1977     3.38    -6.10  0.164     -8.15
##  2 italy    1978     3.39    -6.08  0.0348    -8.11
##  3 italy    1976     3.43    -6.12  0.103     -8.17
##  4 italy    1974     3.50    -6.13 -0.223     -8.26
##  5 italy    1975     3.52    -6.17 -0.0327    -8.22
##  6 spain    1978     3.62    -5.29  0.621     -8.63
##  7 italy    1972     3.63    -6.21 -0.215     -8.38
##  8 italy    1971     3.65    -6.22 -0.148     -8.47
##  9 spain    1977     3.65    -5.30  0.526     -8.73
## 10 italy    1973     3.65    -6.16 -0.325     -8.32
## # … with 332 more rows

arrange()默认按照指定变量升序对所给数据进行重排,使用desc()可以变为按照降序排列。

gasoline %>%
  arrange(desc(lgaspcar))
## # A tibble: 342 x 6
##    country  year lgaspcar lincomep  lrpmg lcarpcap
##    <chr>   <dbl>    <dbl>    <dbl>  <dbl>    <dbl>
##  1 turkey   1966     6.16    -7.51 -0.356    -13.0
##  2 turkey   1960     6.13    -7.80 -0.253    -13.5
##  3 turkey   1961     6.11    -7.79 -0.343    -13.4
##  4 turkey   1962     6.08    -7.84 -0.408    -13.2
##  5 turkey   1968     6.08    -7.42 -0.365    -12.8
##  6 turkey   1963     6.08    -7.63 -0.225    -13.3
##  7 turkey   1964     6.06    -7.63 -0.252    -13.2
##  8 turkey   1967     6.04    -7.46 -0.335    -12.8
##  9 japan    1960     6.00    -6.99 -0.145    -12.2
## 10 turkey   1965     5.82    -7.62 -0.293    -12.9
## # … with 332 more rows

对于已经用group_by()进行了分组的数据,如果想要先对分组变量进行排序需要设定.by_group = TRUE

gasoline %>%
  filter(year %in% seq(1960, 1963)) %>%
  group_by(country) %>%
  arrange(desc(lgaspcar), .by_group = TRUE)
## # A tibble: 72 x 6
## # Groups:   country [18]
##    country  year lgaspcar lincomep  lrpmg lcarpcap
##    <chr>   <dbl>    <dbl>    <dbl>  <dbl>    <dbl>
##  1 austria  1960     4.17    -6.47 -0.335    -9.77
##  2 austria  1961     4.10    -6.43 -0.351    -9.61
##  3 austria  1962     4.07    -6.41 -0.380    -9.46
##  4 austria  1963     4.06    -6.37 -0.414    -9.34
##  5 belgium  1960     4.16    -6.22 -0.166    -9.41
##  6 belgium  1961     4.12    -6.18 -0.172    -9.30
##  7 belgium  1962     4.08    -6.13 -0.222    -9.22
##  8 belgium  1963     4.00    -6.09 -0.250    -9.11
##  9 canada   1960     4.86    -5.89 -0.972    -8.38
## 10 canada   1962     4.85    -5.84 -0.979    -8.32
## # … with 62 more rows

对于非远程数据,arrange()在排序时总会把缺失数据排在最后,使用desc()也不会改变缺失数据的排序。这点与R基础包中有类似功能的sort()不同。

相关文章

网友评论

    本文标题:用dplyr包进行数据清理-mutate()和arrange()

    本文链接:https://www.haomeiwen.com/subject/unjdbctx.html