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

Mar*_*ras 19 r categorization dplyr

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

没有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)))
Run Code Online (Sandbox Code Playgroud)

它会完成.但是,我更喜欢在我执行的其他操作的序列中使用某些dplyr函数(mutate我想).chaindata.frame

G. *_*eck 25

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))))
Run Code Online (Sandbox Code Playgroud)

赠送:

                 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
Run Code Online (Sandbox Code Playgroud)

  • 当您在一个 `data.farme` 上执行一系列操作时,它提高了可读性 - 而不是使用嵌套函数,您可以使用 `%.%` 顺序编写它们,因此 - 在 *from-left 中读取代码-to-right* 方式(不是:*从内到外*)。更多信息:http://blog.rstudio.org/2014/01/17/introducing-dplyr/ (3认同)

Mat*_*cho 11

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)
Run Code Online (Sandbox Code Playgroud)

# cut_number() evenly distributes observations into bins by quantile
skewed_tbl %>%
    ggplot(aes(counts_cut_number)) +
    geom_bar()
Run Code Online (Sandbox Code Playgroud)

# cut_interval() evenly splits the interval across the range
skewed_tbl %>%
    ggplot(aes(counts_cut_interval)) +
    geom_bar()
Run Code Online (Sandbox Code Playgroud)

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

reprex包创建于2018-11-01 (v0.2.1)