rvariablessplitcontinuous

Splitting a continuous variable into equal sized groups


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

Example data frame:

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))

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.


Solution

  • try this:

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

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

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

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

    UPDATED

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

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

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

    das$group <- as.numeric(cut(das$anim, 3))
    

    UPDATED AGAIN

    try this:

    > 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