Example: I have a dataset of heights by gender.
I'd like to split the heights into low and high where the cut points are defined as the mean - 2sd within each gender.
df = data.frame(sex = c(rep("M",100), rep("F",100)),
ht = c(rnorm(100, mean=1.7, sd=.17), rnorm(100, mean=1.6, sd=.16)))
Just discovered the following solution using base r:
df$ht_grp <- ave(x = df$ht, df$sex, FUN = function(x) cut(x, breaks = c(0, (mean(x, na.rm=T) - 2*sd(x, na.rm=T)), Inf)))
This works because I know that 0 and Inf are reasonable bounds, but I could also use
max(x) as my upper and lower bounds. This results in a factor variable that is split into low, high, and NA.
My prior solution: I came up with the following two-step process which is not so bad:
df = merge(df, setNames( aggregate(ht ~ sex, df, FUN = function(x) mean(x)-2*sd(x)), c("sex", "ht_cutoff")), by = "sex") df$ht_is_low = ifelse(df$ht <= df$ht_cutoff, 1, 0)