使用mutate对数值变量进行分类

33

我想使用dplyr对我的data.frame对象中的数值变量进行分类(但我不知道该如何操作)。

如果没有dplyr,我可能会做类似以下的事情:

df <- data.frame(a = rnorm(1e3), b = rnorm(1e3))
df$a <- cut(df$a , breaks=quantile(df$a, probs = seq(0, 1, 0.2)))

这个任务可以完成。但我强烈推荐在对 data.frame 进行其他操作的 chain 序列中使用一些 dplyr 函数(我猜测是mutate)。


1
猜测一下(从谷歌和阅读在线手册得出,我从未使用过 dplyr),我会说 mutate( df , a = cut( a , breaks = quantile( a , probs = seq( 0 , 1 , 0.2 ) ) ) )... - Simon O'Hanlon
2个回答

41

ggplot2包有3个函数非常适合这些任务:

  • cut_number():使用(大约)相等数量的观测值创建n个组
  • cut_interval():使n个组具有相等的范围
  • cut_width():使宽度为width的组

我首选cut_number(),因为它使用均匀间隔的分位数对观测值进行分组。以下是一个偏斜数据的示例。

library(tidyverse)

skewed_tbl <- tibble(
    counts = c(1:100, 1:50, 1:20, rep(1:10, 3), 
               rep(1:5, 5), rep(1:2, 10), rep(1, 20))
    ) %>%
    mutate(
        counts_cut_number   = cut_number(counts, n = 4),
        counts_cut_interval = cut_interval(counts, n = 4),
        counts_cut_width    = cut_width(counts, width = 25)
        ) 

# Data
skewed_tbl
#> # A tibble: 265 x 4
#>    counts counts_cut_number counts_cut_interval counts_cut_width
#>     <dbl> <fct>             <fct>               <fct>           
#>  1      1 [1,3]             [1,25.8]            [-12.5,12.5]    
#>  2      2 [1,3]             [1,25.8]            [-12.5,12.5]    
#>  3      3 [1,3]             [1,25.8]            [-12.5,12.5]    
#>  4      4 (3,13]            [1,25.8]            [-12.5,12.5]    
#>  5      5 (3,13]            [1,25.8]            [-12.5,12.5]    
#>  6      6 (3,13]            [1,25.8]            [-12.5,12.5]    
#>  7      7 (3,13]            [1,25.8]            [-12.5,12.5]    
#>  8      8 (3,13]            [1,25.8]            [-12.5,12.5]    
#>  9      9 (3,13]            [1,25.8]            [-12.5,12.5]    
#> 10     10 (3,13]            [1,25.8]            [-12.5,12.5]    
#> # ... with 255 more rows

summary(skewed_tbl$counts)
#>    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
#>    1.00    3.00   13.00   25.75   42.00  100.00

# Histogram showing skew
skewed_tbl %>%
    ggplot(aes(counts)) +
    geom_histogram(bins = 30)

# cut_number() evenly distributes observations into bins by quantile
skewed_tbl %>%
    ggplot(aes(counts_cut_number)) +
    geom_bar()

# cut_interval() evenly splits the interval across the range
skewed_tbl %>%
    ggplot(aes(counts_cut_interval)) +
    geom_bar()

# cut_width() uses the width = 25 to create bins that are 25 in width
skewed_tbl %>%
    ggplot(aes(counts_cut_width)) +
    geom_bar()

这段文字是由reprex package(v0.2.1)在2018年11月01日创建的。


1
我不知道这些函数。它们能直接在 dplyr 链中使用吗? - elliot
@elliot 在 mutate() 中使用它们。我展示了一个在 dplyr 链中使用所有三个的例子,用于 mutate()。 - Matt Dancho

34
set.seed(123)
df <- data.frame(a = rnorm(10), b = rnorm(10))

df %>% mutate(a = cut(a, breaks = quantile(a, probs = seq(0, 1, 0.2))))

给予:

                 a          b
1  (-0.586,-0.316]  1.2240818
2   (-0.316,0.094]  0.3598138
3      (0.68,1.72]  0.4007715
4   (-0.316,0.094]  0.1106827
5     (0.094,0.68] -0.5558411
6      (0.68,1.72]  1.7869131
7     (0.094,0.68]  0.4978505
8             <NA> -1.9666172
9   (-1.27,-0.586]  0.7013559
10 (-0.586,-0.316] -0.4727914

使用 df %.% mutate( a = ... ) 相较于 df <- mutate( df , a = ... )dplyr 好处是什么?第一种方式是否会通过引用进行更改? - Simon O'Hanlon
5
使用%.%可以使代码按照从左到右的顺序(而非内部到外部)阅读,这样你就可以在执行一系列对于data.frame的操作时提高可读性,而不是使用嵌套函数。详情请参见:http://blog.rstudio.org/2014/01/17/introducing-dplyr/。 - Marta Karas

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接