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

Using a condition related to a part of word in r

I have a dataframe like this:

1                      2 
aquiles_alcatra_Mat_15 picanha
aquiles_alcatra_Mat_15 picanha
alcatra_Mat_15         picanha
alcatra_Mat_20         picanha
alcatra_Mat_25         picanha
picanha_Mat_20         picanha
picanha_Mat_25         picanha

and I would like to do this:

1                      2 
aquiles_alcatra_Mat_15 alcatra
aquiles_alcatra_Mat_15 alcatra
alcatra_Mat_15         alcatra
alcatra_Mat_20         alcatra
alcatra_Mat_25         alcatra
picanha_Mat_20         picanha
picanha_Mat_25         picanha

If I have alcatra as a part of the word on column 1 I will just want alcatra on column 2.

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

I know I can use this:

file$2[ file$1 == 'aquiles_alcatra_Mat_15'] <- 'alcatra'

but I have several situations almost 300. And I tried something like this:

file$2[ file$1 == '.*alcatra*'] <- 'alcatra'

But did’nt work.

>Solution :

A possible solution, based on dplyr and stringr::str_detect:

library(tidyverse)

df %>% 
  mutate(V2 = if_else(str_detect(V1, "alcatra"), "alcatra", V2))

#>                       V1      V2
#> 1 aquiles_alcatra_Mat_15 alcatra
#> 2 aquiles_alcatra_Mat_15 alcatra
#> 3         alcatra_Mat_15 alcatra
#> 4         alcatra_Mat_20 alcatra
#> 5         alcatra_Mat_25 alcatra
#> 6         picanha_Mat_20 picanha
#> 7         picanha_Mat_25 picanha
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