【问题标题】:Categorize numeric variable with mutate使用 mutate 对数值变量进行分类
【发布时间】:2014-06-03 12:30:00
【问题描述】:

我想使用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)来实现这一点。

【问题讨论】:

  • 猜测(来自谷歌并阅读在线手册,我从未使用过dplyr)我会说mutate( df , a = cut( a , breaks = quantile( a , probs = seq( 0 , 1 , 0.2 ) ) ) )...

标签: r dplyr categorization


【解决方案1】:

ggplot2 包有 3 个函数可以很好地完成这些任务:

  • cut_number(): 用(大约)相等数量的观察创建 n 个组
  • cut_interval(): 使 n 个组的范围相等
  • cut_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 月 1 日创建

【讨论】:

  • 我不知道这些功能。可以直接在dplyr链中使用吗?
  • @elliot 在mutate() 中使用它们。我展示了一个使用 mutate() 在 dplyr 链中使用所有三个的示例。
【解决方案2】:
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

【讨论】:

  • dplyrdf &lt;- mutate( df , a = ... ) 相比,df %.% mutate( a = ... ) 的好处是什么。第一种方式是否通过引用而改变?
  • 当您在一个 data.farme 上执行一系列操作时,它提高了可读性 - 您可以使用 %.% 顺序编写它们,而不是使用嵌套函数,因此 - 读取代码从左到右方式(不是:从内到外)。更多信息:blog.rstudio.org/2014/01/17/introducing-dplyr
猜你喜欢
  • 2011-01-31
  • 2020-02-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-02-10
  • 1970-01-01
  • 2021-05-05
相关资源
最近更新 更多