Classify rows depending on case of others variable

I have a large base and I want to classify the different case we have in fonction of a defined decision tree. For example where resultatD1 = I and diffcd1<3 then data$classification <-"KI". I try to do it with a if :

if (data$final.resultatD1=="I" && diffcd1<3)data$class<-"KI"

and I also though to use a sql procedure but I don't know how to write in an sql procedure :

sqldf("SELECT idR, resultatD1, diffcd1
      FROM data
      WHERE resultatD1='I' and diffcd1<3")

So I don't know how to deal with this problem. Also, I have variable resultatD1 to resultatD7, do you know how can I do to repeat the code for this 7 variable?

I hope I was clear, thank you for your time.

I found something like this, i think it works, do you know how I can integrate an iteration of my variable from resultatD1 to resultatD7 and diff d1 to diffcd7?

data<-data%>% 
   mutate(class = case_when(
     data$resultatD1 == 'I' & data$diffcd1>0 &data$diffcd1<3 ~ "KI",
      TRUE ~ "other"
   )
   )

Hello, your issue is likely that if() is not vectorised, so r provides ifelse() for that requirement.

There is a good tutorial to look at here :
adv-r

If you require more direct help with your issue, please consider providing a reprex

I have to do one of my assignement in fonction of if one variable is emtpy (NA) but I don't succeed to deal with it

data<-data%>% 
   mutate(class = case_when(
     data$resultatD1 == 'I' & **data$date =='NA'** &data$diffcd1<3 ~ "KI",
      TRUE ~ "other"
   )
   )

I tried various things like " " or NULL or NA for exemple but I don't know what syntax I should use.

r provides the function is.na() to allow for TRUE / FALSE detection of whether a value is an NA

also, you are mixing base syntax and tidyverse syntax, and while there is a time and place for the careful doing of that, your example is not one of those.
Note that you are mutating data so mutate knows the variables in data, and does not require data$ to pick them, therefore, eliminate data$ from your mutate code.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.