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.
A Median Split is one method for turning a continuous variable into a categorical one. Essentially, the idea is to find the median of the continuous variable. Any value below the median is put it the category “Low” and every value above it is labeled “High.”
1 Answer. Continuous variables in R can be split into different groups or ranks by use of the function cut().
You can use the cut() function in R to create a categorical variable from a continuous one. Note that breaks specifies the values to split the continuous variable on and labels specifies the label to give to the values of the new categorical variable.
Divide the Data into Groups in R Programming – split() function. split() function in R Language is used to divide a data vector into groups as defined by the factor provided.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With