Home > Enterprise >  Partial string match over two columns R
Partial string match over two columns R

Time:04-06

I have a large df (only 2 columns here for example)

CancerVar<-c("CancerVar:9#Tier_II_potential","CancerVar:2#Tier_IV_benign","CancerVar:11#Tier_I_strong","CancerVar:2#Tier_IV_benign","CancerVar:2#Tier_IV_benign")
driver_mut_prediction<-c("not protein-affecting","TIER 1","passenger","TIER 2","passenger")
df<-data.frame(CancerVar,driver_mut_prediction)


  df
                      CancerVar driver_mut_prediction
1 CancerVar:9#Tier_II_potential not protein-affecting
2    CancerVar:2#Tier_IV_benign                TIER 1
3    CancerVar:11#Tier_I_strong             passenger
4    CancerVar:2#Tier_IV_benign                TIER 2
5    CancerVar:2#Tier_IV_benign             passenger

I want to select rows using partial (different) string matches over two columns. I want to select rows where EITHER (CancerVar contains Tier I OR Tier II) OR (driver_mut_prediction contains TIER 1 OR TIER 2)

I have tried:

df_sub<-df[with(df, grepl("TIER|Tier_I|Tier_II", paste(driver_mut_prediction, CancerVar,ignore.case=FALSE))),]

Which still has the last row (so neither conditional has worked)

I have tried:

df %>% select(contains("Tier_I|Tier_II|TIER 1|TIER 2"))

data frame with 0 columns and 5000 rows

Please help!

CodePudding user response:

This approach should work:

library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
CancerVar<-c("CancerVar:9#Tier_II_potential","CancerVar:2#Tier_IV_benign","CancerVar:11#Tier_I_strong","CancerVar:2#Tier_IV_benign","CancerVar:2#Tier_IV_benign")
driver_mut_prediction<-c("not protein-affecting","TIER 1","passenger","TIER 2","passenger")
df<-data.frame(CancerVar,driver_mut_prediction)

df %>%
  filter(
    grepl("Tier_I_|Tier_II_", CancerVar) |
    grepl("TIER 1|TIER 2", driver_mut_prediction)
   )
#>                       CancerVar driver_mut_prediction
#> 1 CancerVar:9#Tier_II_potential not protein-affecting
#> 2    CancerVar:2#Tier_IV_benign                TIER 1
#> 3    CancerVar:11#Tier_I_strong             passenger
#> 4    CancerVar:2#Tier_IV_benign                TIER 2

Created on 2022-04-06 by the reprex package (v2.0.1)

Or, with base R:

CancerVar<-c("CancerVar:9#Tier_II_potential","CancerVar:2#Tier_IV_benign","CancerVar:11#Tier_I_strong","CancerVar:2#Tier_IV_benign","CancerVar:2#Tier_IV_benign")
driver_mut_prediction<-c("not protein-affecting","TIER 1","passenger","TIER 2","passenger")
df<-data.frame(CancerVar,driver_mut_prediction)

df[grepl("Tier_I_|Tier_II_", df$CancerVar) | grepl("TIER 1|TIER 2", df$driver_mut_prediction),]
#>                       CancerVar driver_mut_prediction
#> 1 CancerVar:9#Tier_II_potential not protein-affecting
#> 2    CancerVar:2#Tier_IV_benign                TIER 1
#> 3    CancerVar:11#Tier_I_strong             passenger
#> 4    CancerVar:2#Tier_IV_benign                TIER 2

Created on 2022-04-06 by the reprex package (v2.0.1)

CodePudding user response:

You can use str_detect:

library(tidyverse)

df %>% 
  filter(str_detect(CancerVar, "Tier_I_|Tier_II_") | 
           str_detect(driver_mut_prediction, "TIER 1|TIER 2"))

Output

                      CancerVar driver_mut_prediction
1 CancerVar:9#Tier_II_potential not protein-affecting
2    CancerVar:2#Tier_IV_benign                TIER 1
3    CancerVar:11#Tier_I_strong             passenger
4    CancerVar:2#Tier_IV_benign                TIER 2

Data

df <- structure(list(CancerVar = c("CancerVar:9#Tier_II_potential", 
"CancerVar:2#Tier_IV_benign", "CancerVar:11#Tier_I_strong", "CancerVar:2#Tier_IV_benign", 
"CancerVar:2#Tier_IV_benign"), driver_mut_prediction = c("not protein-affecting", 
"TIER 1", "passenger", "TIER 2", "passenger")), class = "data.frame", row.names = c(NA, 
-5L))
  •  Tags:  
  • r
  • Related