将连续变量拆分为大小相等的组 [英] Splitting a continuous variable into equal sized groups

查看:54
本文介绍了将连续变量拆分为大小相等的组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将一个连续变量分成 3 个大小相等的组.

I need to split/divide up a continuous variable into 3 equal sized groups.

示例数据框:

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个类,如下所示:

After being cut up (according to the value of wt), I would need to have the 3 classes under the new variable wt2 like this:

> 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

这将应用于大型数据集.

This would be applied to a large data set.

推荐答案

试试这个:

split(das, cut(das$anim, 3))

如果要根据wt的值进行拆分,则

if you want to split based on the value of wt, then

library(Hmisc) # cut2
split(das, cut2(das$wt, g=3))

无论如何,你可以通过组合cutcut2split来做到这一点.

anyway, you can do that by combining cut, cut2 and split.

更新

如果你想要一个组索引作为附加列,那么

if you want a group index as an additional column, then

das$group <- cut(das$anim, 3)

如果该列的索引应该是 1, 2, ..., 那么

if the column should be index like 1, 2, ..., then

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

这篇关于将连续变量拆分为大小相等的组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆