Is there a R code that can detect and separate strings containing characters that are similar?

21 Views Asked by At

enter image description here

I have a dataset (with 100k rows) with strings that contain almost similar names, but I need to separate the names.

fertilizer_name<-c('Urea (inorganic)', 'solid manure (organic)')
df1<-data.frame(fertilizer_name)

I have tried the grep function, but since organic is contained in the Inorganic, then it filters both organic and inorganic as follows


df1 %>% filter(grepl('Organic', fertilizer_name))
1

There are 1 best solutions below

0
stefan_aus_hannover On BEST ANSWER

Just exclude the inorganic

df1 %>% filter(grepl('Organic', fertilizer_name) & !grepl('Inorganic', fertilizer_name))