Create new column based of subset of strings in another column in R?

I am trying to create new a column in my dataframe, based off a subset of strings in another
This is my dataframe

df =structure(list(Combination = c("BRUV_Acoustic_Satellite", "BRUV_Acoustic_Satellite", 
"BRUV_Acoustic_Satellite", "BRUV_Acoustic_Satellite", "BRUV_Acoustic_Satellite", 
"BRUV_Acoustic_Satellite", "Animalborne_Archival", "Animalborne_Archival", 
"Animalborne_Archival", "Controlled_Acoustic", "Controlled_Acoustic", 
"Controlled_Acoustic", "Controlled_Acoustic", "Controlled_Acoustic", 
"Controlled_Acoustic", "Animalborne_Archival", "Animalborne_Archival", 
"Animalborne_Archival", "Stationary_Radio", "Stationary_Radio", 
"Stationary_Radio", "Animalborne_Satellite_Archival", "Animalborne_Satellite_Archival", 
"Animalborne_Satellite_Archival", "Animalborne_Satellite_Archival", 
"Stationary_Acoustic", "Stationary_Acoustic", "Stationary_Acoustic", 
"Stationary_Acoustic", "Stationary_Acoustic", "Stationary_Acoustic", 
"BRUV_Acoustic_Satellite", "BRUV_Acoustic_Satellite", "BRUV_Acoustic_Satellite", 
"Stationary_Archival", "Stationary_Archival", "Stationary_Archival", 
"Stationary_Archival", "Stationary_Acoustic_Radio_PIT", "Stationary_Acoustic_Radio_PIT", 
"Stationary_Acoustic_Radio_PIT", "Controlled_Acoustic", "Controlled_Acoustic", 
"Stationary_PIT", "Stationary_PIT", "Stationary_Acousitc_PIT", 
"Stationary_Acousitc_PIT", "Stationary_Acousitc_PIT", "BRUV_Acoustic", 
"BRUV_Acoustic", "BRUV_Acoustic", "Stationary_Acoustic", "Stationary_Acoustic", 
"Stationary_Acoustic", "Stationary_Acoustic", "Stationary_Acoustic", 
"Stationary_Acoustic", "Stationary_Acoustic", "Stationary_Acoustic", 
"Stationary_Acoustic", "Stationary_Acoustic", "Stationary_Archival", 
"Stationary_Archival", "Stationary_Archival", "Stationary_Archival", 
"Stationary_Satellite", "Controlled_Acoustic", "Controlled_Acoustic", 
"Controlled_Acoustic", "Controlled_Acoustic", "BRUV_Acoustic", 
"BRUV_Acoustic", "BRUV_Acoustic", "Animalborne_Satellite", "Animalborne_Satellite", 
"Stationary_Archival", "Stationary_Archival", "Stationary_Archival", 
"Stationary_Radio_PIT", "Stationary_Radio_PIT", "Controlled_Acoustic", 
"Controlled_Acoustic", "Controlled_Acoustic", "Controlled_Acoustic", 
"Controlled_Satellite", "Controlled_Satellite", "Controlled_Satellite", 
"Controlled_Satellite", "Animalborne_Archival", "Animalborne_Archival", 
"Animalborne_Archival", "Animalborne_Archival", "Animalborne_Archival", 
"Stationary_Acoustic", "Stationary_Acoustic", "Stationary_Acoustic", 
"Animalborne_Archival", "Animalborne_Archival", "Animalborne_Archival", 
"Animalborne_Archival_PIT", "Animalborne_Archival_PIT", "Animalborne_Archival_PIT", 
"Animalborne_Acoustic_Archival", "Animalborne_Acoustic_Archival", 
"Animalborne_Acoustic_Archival", "Stationary_Acoustic", "Stationary_Acoustic", 
"Stationary_Acoustic", "Animalborne_Archival", "Animalborne_Archival", 
"Animalborne_Archival", "Animalborne_Archival", "Animalborne_Archival", 
"Animalborne_Archival", "Animalborne_Archival", "Animalborne_Archival", 
"Animalborne_Archival", "Animalborne_Archival", "Animalborne_Archival", 
"Animalborne_Archival", "Animalborne_Archival", "Stationary_Acoustic_Archival", 
"Stationary_Acoustic_Archival", "Stationary_Acoustic_Archival", 
"Stationary_Acoustic", "Stationary_Acoustic", "Stationary_Acoustic", 
"Stationary_Acoustic", "Stationary_Acoustic", "Stationary_Acoustic", 
"Stationary_Acoustic", "Stationary_Acoustic", "Stationary_Acoustic", 
"Stationary_Acoustic", "Stationary_Acoustic", "Animalborne_Archival", 
"Animalborne_Archival", "Animalborne_Archival", "Animalborne_Archival", 
"Stationary_Acoustic_Archival", "Stationary_Acoustic_Archival", 
"Stationary_Acoustic_Archival", "Stationary_Acoustic_Archival", 
"Animalborne_Acoustic", "Animalborne_Acoustic", "Animalborne_Acoustic", 
"Animalborne_Archival", "Animalborne_Archival", "Stationary_Acoustic_PIT", 
"Stationary_Acoustic_PIT", "Stationary_Acoustic_PIT", "BRUV_Acoustic", 
"BRUV_Acoustic", "BRUV_Acoustic", "BRUV_Acoustic", "BRUV_Acoustic", 
"BRUV_Acoustic", "Controlled_Archival", "Controlled_Archival", 
"Controlled_Archival", "Animalborne_Archival", "Animalborne_Archival", 
"Animalborne_Archival", "Animalborne_Archival", "Animalborne_Archival", 
"Animalborne_Archival", "Animalborne_Archival", "Animalborne_Archival", 
"Animalborne_Archival", "Animalborne_Archival", "Stationary_Radio", 
"Stationary_Acoustic_Archival", "Animalborne_Archival", "Animalborne_Archival", 
"Animalborne_Archival", "Animalborne_Archival", "Animalborne_Archival", 
"Animalborne_Archival", "Stationary_Acoustic_Archival", "Stationary_Acoustic_Archival", 
"Stationary_Acoustic_Archival", "Controlled_Acoustic", "Controlled_Acoustic", 
"Animalborne_Archival", "Animalborne_Archival", "Stationary_Acoustic", 
"Stationary_Acoustic", "Animalborne_Satellite_Archival", "Animalborne_Satellite_Archival", 
"Animalborne_Satellite_Archival", "Animalborne_Satellite_Archival", 
"Stationary_Acoustic", "Stationary_Acoustic", "Stationary_Acoustic", 
"Stationary_Acoustic", "Animalborne_Archival", "Animalborne_Archival", 
"Animalborne_Archival", "Stationary_Satellite", "Stationary_Satellite", 
"Stationary_Satellite", "Stationary_Satellite", "Stationary_Satellite", 
"Animalborne_Archival", "Animalborne_Archival", "Stationary_Acoustic_Radio", 
"Stationary_Acoustic_Radio", "Stationary_Acoustic_Radio", "Animalborne_Archival", 
"Animalborne_Archival", "Animalborne_Archival", "Animalborne_Archival", 
"Stationary_Acoustic", "Stationary_Acoustic", "Animalborne_Archival", 
"Animalborne_Archival", "Animalborne_Archival", "Animalborne_Archival", 
"Animalborne_Archival", "Animalborne_Archival", "Animalborne_Archival", 
"Animalborne_Archival", "Animalborne_Archival", "Animalborne_Archival", 
"Animalborne_Archival", "BRUV_Acoustic", "BRUV_Acoustic", "BRUV_Acoustic", 
"BRUV_Acoustic", "BRUV_Acoustic", "Stationary_Acoustic", "Stationary_Acoustic", 
"Stationary_Acoustic", "Stationary_Acoustic", "Stationary_Acoustic", 
"Stationary_Acoustic", "Stationary_Acoustic", "Stationary_Acoustic", 
"Stationary_Acoustic", "Animalborne_Satellite_Archival", "Animalborne_Satellite_Archival", 
"Animalborne_Satellite_Archival", "Controlled_Acoustic", "Controlled_Acoustic", 
"Controlled_Acoustic")), class = "data.frame", row.names = c(NA, 
-245L))

I want a new column where the values relate to some strings so for every value that has a Acoustic, radio, or PIT the value in the new column should be receiver based and for everything else it should be non receiver based. But for columns that have acoustic and satellite in the column I need the value in the new column to say Both.

I have tried the ifelse command using this code

df$Type = ifelse(df$Combination == "Acoustic", 'Non Receiver Based', 'Receiver Based')

But it’s calling all of them receiver based and I don’t know how to incorporate all the arguments I am suggesting above.

>Solution :

You can use grepl() to check if a certain pattern occurs in a string, and then use if-else statements to decide on your cases. Since if() is not vectorized, you need to wrap it in Vectorize() to use it in mutate().

library(tidyverse)

match <- Vectorize(function(string) {
  if (!grepl("Acoustic|radio|PIT", string)) {
    "non receiver based"
  } else if ((grepl("Acoustic", string)) &
             (grepl("Satellite", string))) {
    "Both"
  } else "receiver based"
})

df %>% mutate(new_var = match(Combination))

Leave a Reply