I am trying to classify a dataframe with cascading criteria using the tidyverse logic (I am trying to learn it). I can do it with base R but can't do it with tidyverse- I found some examples using an hybrid approach tidyverse+base r (using subset) but can't find/understand how to do it using only the dplyr/tidyverse grammar (filter, mutate).
The problem is that, after subsetting for the first criterium (using filter) , the dataframe contains only the filtered rows, and I am not able to subset and classify applying the remaining criteria. I can probably use a temporary df and rbind() but I think there could be a more elegant way to do it using only the tidyverse grammar. In short I would like to update ONLY the rows matching my criteria, with all the other rows left untouched in the originary DF. I should do it using the dplyr grammar. Is that possible?
# with base R
mydata$mytype = "NA"
mydata$mytype[which(mydata$field1 > 300)] = "type1"
mydata$mytype[which(mydata$field1 <= 300 & mydata$field1 > 200)] = "type2"
# with dplyr/tidyverse?
library(tidyverse)
mydata<-mydata%>% mutate(mytype = "NA")
mydata<-mydata%>%filter(field1>300) %>% mutate(mytype="type1")
mydata<-mydata%>%filter(field1 >200, field1<=300) %>% mutate(mytype="type2") #0 rows now