mercredi 30 janvier 2019

How to create a column requiring more than 50 different factor values in R using ifelse statement[read comments]

I am using ifelse statements more than 100 times to create >100 visit categories as shown in below .

df<-data.frame(visit_time=c(12,20,70,100),city=c("X","X","X","X"))

df$visit_category<-ifelse(df$visit_time>=0 & df$visit_time<12 ,"0-12",ifelse(df$visit_time>=12 & df$visit_time <20,"12-20",ifelse(df$visit_time>=20 & df$visit_time <50,"20-50",ifelse(df$visit_time>=50 & df$visit_time <100,"50-100",">100"))))

As shown in sample data [ visit category column] , I have 4 different values for sample data , and I need to create >50 different categories on actual data.

When I try to create more than 50 categories , I get an error in R like this [ Did some research n found out that R does not allow more than 50 iflese statements at the same time !!, Dont know if its true ]

#Error: unexpected ')' in
#Error: contextstack overflow at line 42

Is there any better way to create >50 different visit category values other than using ifelse statements in R ??

Aucun commentaire:

Enregistrer un commentaire