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

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

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

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))
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