将连续变量分成相等大小的组

| 我需要将连续变量拆分/划分为3个大小相等的组。 示例数据框
das <- data.frame(anim=1:15,
                  wt=c(181,179,180.5,201,201.5,245,246.4,
                       189.3,301,354,369,205,199,394,231.3))
切掉后(根据
wt
的值),我需要在新变量
wt2
下有3个类,如下所示:
> das 
   anim    wt wt2
1     1 181.0   1
2     2 179.0   1
3     3 180.5   1
4     4 201.0   2
5     5 201.5   2
6     6 245.0   2
7     7 246.4   3
8     8 189.3   1
9     9 301.0   3
10   10 354.0   3
11   11 369.0   3
12   12 205.0   2
13   13 199.0   1
14   14 394.0   3
15   15 231.3   2
这将适用于大型数据集     
已邀请:
尝试这个:
split(das, cut(das$anim, 3))
如果要基于ѭ1的值进行分割,则
library(Hmisc) # cut2
split(das, cut2(das$wt, g=3))
无论如何,您可以通过组合
cut
cut2
split
来做到这一点。 更新 如果要将组索引作为附加列,则
das$group <- cut(das$anim, 3)
如果该列的索引应为1,2,...,则
das$group <- as.numeric(cut(das$anim, 3))
再次更新 尝试这个:
> das$wt2 <- as.numeric(cut2(das$wt, g=3))
> das
   anim    wt wt2
1     1 181.0   1
2     2 179.0   1
3     3 180.5   1
4     4 201.0   2
5     5 201.5   2
6     6 245.0   2
7     7 246.4   3
8     8 189.3   1
9     9 301.0   3
10   10 354.0   3
11   11 369.0   3
12   12 205.0   2
13   13 199.0   1
14   14 394.0   3
15   15 231.3   2
    
或参见
ggplot2
包中的
cut_number
,例如
das$wt_2 <- as.numeric(cut_number(das$wt,3))
注意,“ 16”将原始数据的范围分为三个等长的范围;如果数据分布不均匀,则不一定会导致每个组的观察次数相同(您可以通过适当地使用
quantile
来复制
cut_number
的操作,但这是一个很好的便捷功能)。另一方面,使用
g=
参数的ѭ19does确实被分位数分割,所以或多或少地等同于
ggplot2::cut_number
。我可能以为到目前为止,类似ѭ13的东西会变成ѭ23的东西,但是据我所知还没有。     
这是使用mltools软件包中的
bin_data()
函数的另一种解决方案。
library(mltools)

# Resulting bins have an equal number of observations in each group
das[, \"wt2\"] <- bin_data(das$wt, bins=3, binType = \"quantile\")

# Resulting bins are equally spaced from min to max
das[, \"wt3\"] <- bin_data(das$wt, bins=3, binType = \"explicit\")

# Or if you\'d rather define the bins yourself
das[, \"wt4\"] <- bin_data(das$wt, bins=c(-Inf, 250, 322, Inf), binType = \"explicit\")

das
   anim    wt                                  wt2                                  wt3         wt4
1     1 181.0              [179, 200.333333333333)              [179, 250.666666666667) [-Inf, 250)
2     2 179.0              [179, 200.333333333333)              [179, 250.666666666667) [-Inf, 250)
3     3 180.5              [179, 200.333333333333)              [179, 250.666666666667) [-Inf, 250)
4     4 201.0 [200.333333333333, 245.466666666667)              [179, 250.666666666667) [-Inf, 250)
5     5 201.5 [200.333333333333, 245.466666666667)              [179, 250.666666666667) [-Inf, 250)
6     6 245.0 [200.333333333333, 245.466666666667)              [179, 250.666666666667) [-Inf, 250)
7     7 246.4              [245.466666666667, 394]              [179, 250.666666666667) [-Inf, 250)
8     8 189.3              [179, 200.333333333333)              [179, 250.666666666667) [-Inf, 250)
9     9 301.0              [245.466666666667, 394] [250.666666666667, 322.333333333333)  [250, 322)
10   10 354.0              [245.466666666667, 394]              [322.333333333333, 394]  [322, Inf]
11   11 369.0              [245.466666666667, 394]              [322.333333333333, 394]  [322, Inf]
12   12 205.0 [200.333333333333, 245.466666666667)              [179, 250.666666666667) [-Inf, 250)
13   13 199.0              [179, 200.333333333333)              [179, 250.666666666667) [-Inf, 250)
14   14 394.0              [245.466666666667, 394]              [322.333333333333, 394]  [322, Inf]
15   15 231.3 [200.333333333333, 245.466666666667)              [179, 250.666666666667) [-Inf, 250)
    
现在从
dplyr
中的
ntile
开始这样做,但是对于with28ѭ\'s的表现却很奇怪。 我在下面的函数中使用了类似的代码,该函数在base R中起作用,并且等效于上面的
cut2
解决方案:
ntile_ <- function(x, n) {
    b <- x[!is.na(x)]
    q <- floor((n * (rank(b, ties.method = \"first\") - 1)/length(b)) + 1)
    d <- rep(NA, length(x))
    d[!is.na(x)] <- q
    return(d)
}
    
不使用cut2的替代方法。
das$wt2 <- as.factor( as.numeric( cut(das$wt,3)))
要么
das$wt2 <- as.factor( cut(das$wt,3, labels=F))
正如@ ben-bolker指出的那样,它分成相等的宽度而不是占用率。 我认为使用
quantiles
可以近似占用
x = rnorm(10)
x
 [1] -0.1074316  0.6690681 -1.7168853  0.5144931  1.6460280  0.7014368
 [7]  1.1170587 -0.8503069  0.4462932 -0.1089427
bin = 3 #for 1/3 rd, 4 for 1/4, 100 for 1/100th etc
xx = cut(x, quantile(x, breaks=1/bin*c(1:bin)), labels=F, include.lowest=T)
table(xx)
1 2 3 4
3 2 2 3
    
cut
,如果未指定明确的断点,则将值分成相同宽度的bin,通常它们将不包含相等数量的项:
x <- c(1:4,10)
lengths(split(x, cut(x, 2)))
# (0.991,5.5]    (5.5,10] 
#           4           1 
Hmisc::cut2
ggplot2::cut_number
使用分位数,如果数据分布良好且大小合适,通常将创建相同大小的组(就元素数量而言),但是并非总是如此。
mltools::bin_data
可以给出不同的结果,但也基于分位数。 当数据包含少量不同值时,这些函数并不总是给出整洁的结果:
x <- rep(c(1:20),c(15, 7, 10, 3, 9, 3, 4, 9, 3, 2,
                   23, 2, 4, 1, 1, 7, 18, 37, 6, 2))

table(x)
# x
#  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 
# 15  7 10  3  9  3  4  9  3  2 23  2  4  1  1  7 18 37  6  2   

table(Hmisc::cut2(x, g=4))
# [ 1, 6) [ 6,12) [12,19) [19,20] 
#      44      44      70       8

table(ggplot2::cut_number(x, 4))
# [1,5]  (5,11] (11,18] (18,20] 
#    44      44      70       8

table(mltools::bin_data(x, bins=4, binType = \"quantile\"))
# [1, 5)  [5, 11) [11, 18) [18, 20] 
#     35       30       56       45
如果在此处找到最佳解决方案,则尚不清楚。 最好的分箱方法是一个主观问题,但是一种合理的解决方法是寻找使期望的分箱大小周围的方差最小的分箱。 (my)软件包
cutr
中的函数
smart_cut
提出了这样的功能。但是,它的计算量很大,应该保留给切点和唯一值很少的情况(通常在这种情况下很重要)。
# devtools::install_github(\"moodymudskipper/cutr\")
table(cutr::smart_cut(x, list(4, \"balanced\"), \"g\"))
# [1,6)  [6,12) [12,18) [18,20] 
# 44      44      33      45 
我们看到小组之间的平衡要好得多。 如果基于差异的方法不够用,则调用中的“ 44”实际上可以由自定义函数代替,以根据需要优化或限制垃圾箱。     
funModeling
中的
equal_freq
取一个向量和箱数(基于相等频率):
das <- data.frame(anim=1:15,
                  wt=c(181,179,180.5,201,201.5,245,246.4,
                       189.3,301,354,369,205,199,394,231.3))

das$wt_bin=funModeling::equal_freq(das$wt, 3)

table(das$wt_bin)

#[179,201) [201,246) [246,394] 
#        5         5         5 
    
您也可以将
bin
函数和
OneR
包中的
method = \"content\"
一起使用:
library(OneR)
das$wt_2 <- as.numeric(bin(das$wt, nbins = 3, method = \"content\"))
das
##    anim    wt wt_2
## 1     1 181.0    1
## 2     2 179.0    1
## 3     3 180.5    1
## 4     4 201.0    2
## 5     5 201.5    2
## 6     6 245.0    2
## 7     7 246.4    3
## 8     8 189.3    1
## 9     9 301.0    3
## 10   10 354.0    3
## 11   11 369.0    3
## 12   12 205.0    2
## 13   13 199.0    1
## 14   14 394.0    3
## 15   15 231.3    2
    
没有任何额外的软件包,3为组数:
> findInterval(das$wt, unique(quantile(das$wt, seq(0, 1, length.out = 3 + 1))), rightmost.closed = TRUE)
 [1] 1 1 1 2 2 2 3 1 3 3 3 2 1 3 2
您可以通过使用感兴趣的值的代表性样本来加快分位数计算。仔细检查
FindInterval
函数的文档。     
如果要分成3组,则答案与本·博克的答案相同-使用
ggplot2::cut_number()
。为了完善起见,这里提供了将连续转换为分类(合并)的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软件包(v0.2.1)创建于2018-11-01     

要回复问题请先登录注册