KenR
KenR

Reputation: 31

Grouping numeric values

I have a numeric variable Tot_cd that goes from 1 to 50. I would like to create a copy of that variable (tcd_grp) that groups some of the numbers but leaves 1 and 2 alone e.g. 1, 2, 3 to 5, 6 or more

based on a similar answer I tried using cut as follows:

tcd_grp <- (Tot_cd)
cut(tcd_grp, c(-Inf,1,2,5,Inf), labels =c("1","2","3-5","6 or more"))

this seemed not to work as the frequencies for Tcd_grp were the same as for Tot_cd

Upvotes: 2

Views: 70

Answers (1)

KenR
KenR

Reputation: 31

Question:I have a numeric variable Tot_cd that goes from 1 to 50. I would like to create a copy of that variable (tcd_grp) that groups some of the numbers but leaves 1 and 2 alone e.g. 1, 2, 3 to 5, 6 or more

Solution: First I needed to load the expss package Then the following SPSS like code solved my problem

tcd_grp<-(TCD) recode(tcd_grp, 1 ~ copy, 2:4 ~ 2, 5%thru%hi ~ 5) %into% r_tcd_grp val_lab(r_tcd_grp)=c("1" = 1, "2 to 4" = 2, "5 or more" =5)

Upvotes: 1

Related Questions