Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

r remove keywords in a column

I have a column in my dataframe with words like this.

ColA
2-4 Model
Group1
Group ACH
Group2
Phenols
Group1
Group ACH
Group2
MONO MHPP
Group1
Group ACH
Group2

I want to create two additional columns like this: 1) without keywords c("Group1", "Group (ACH)", "Group2") and 2) a second column that retains only those bag of words.

ColA          ColB        ColC
2-4 Model     2-4 Model   
Group1                    Group1
Group (ACH)               Group (ACH) 
Group2                    Group2
Phenols      Phenols 
Group1                    Group1
Group (ACH)               Group (ACH)
Group2                    Group2
MONO MHPP    MONO MHPP
Group1                    Group1
Group (ACH)               Group (ACH)
Group2                    Group2  

I tried gsub and str_replace but no results. So any suggestion is much appreciated.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You may use an ifelse

val <- c('Group1', 'Group ACH', 'Group2')
df <- transform(df, ColB = ifelse(ColA %in% val, '', ColA), 
                    ColC = ifelse(ColA %in% val, ColA, ''))
df

#        ColA      ColB      ColC
#1  2-4 Model 2-4 Model          
#2     Group1              Group1
#3  Group ACH           Group ACH
#4     Group2              Group2
#5    Phenols   Phenols          
#6     Group1              Group1
#7  Group ACH           Group ACH
#8     Group2              Group2
#9  MONO MHPP MONO MHPP          
#10    Group1              Group1
#11 Group ACH           Group ACH
#12    Group2              Group2

If in general, you want to check for all "Group" values you may use grepl instead of mentioning all of them in val.

df <- transform(df, ColB = ifelse(grepl('Group', ColA), '', ColA), 
                    ColC = ifelse(grepl('Group', ColA), ColA, ''))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading