lundi 27 juillet 2020

Grouping column by the first two characters into two groups

I have this type of DF

DF
N   ID
1   0230
2   5902
3   2359
4   0530

DF
N   ID     GR
1   0230   1
2   5902   2
3   2359   1
4   3030   2

I would like to group column ID by the first two characters into two groups. If first two characters are number lower than 30 is group 1 and higher than 30 and lower than 59 is group 2.

I would prefer dplyr solution.

I have tried this:

DF%>%
mutate(GR = ifelse(str(ID[2] >= 0 & DRG[2] < 31, 1, ifelse(str(ID[2] >= 31 & DRG[2] < 60, 0, "-" )))))

Aucun commentaire:

Enregistrer un commentaire