categorization

R code to categorize age into group/ bins/ breaks

旧城冷巷雨未停 提交于 2019-11-27 04:32:49
I am trying to categorize age into group so it will not be continuous. I have this code: data$agegrp(data$age>=40 & data$age<=49) <- 3 data$agegrp(data$age>=30 & data$age<=39) <- 2 data$agegrp(data$age>=20 & data$age<=29) <- 1 the above code is not working under survival package. It's giving me: invalid function in complex assignment Can you point me where the error is? data is the dataframe I am using. A5C1D2H2I1M1N2O1R2T1 I would use findInterval() here: First, make up some sample data set.seed(1) ages <- floor(runif(20, min = 20, max = 50)) ages # [1] 27 31 37 47 26 46 48 39 38 21 26 25 40

Categorize numeric variable with mutate

久未见 提交于 2019-11-27 01:07:41
问题 I would like to a categorize numeric variable in my data.frame object with the use of dplyr (and have no idea how to do it). Without dplyr , I would probably do something like: df <- data.frame(a = rnorm(1e3), b = rnorm(1e3)) df$a <- cut(df$a , breaks=quantile(df$a, probs = seq(0, 1, 0.2))) and it would be done. However, I strongly prefer to do it with the use of some dplyr function ( mutate , I suppose) in the chain sequence of other actions I do perform over my data.frame . 回答1: set.seed