2

I am trying to turn specific string patterns into binary columns for three different columns using the R programming language.

Here is what I have:

have <- structure(list(rep1 = c("china", "na", "bay", "eng", "giad", 
"china", "sing", "giad", "na", "china", "china, camp", "guat,camp", 
"na", "na", "cis", "trans", "stron, mon"), rep2 = c("china", 
"na", "bay", "eng", "giad", "china", "sing", "giad", "na", "china", 
"china, camp", "camp", "na", "na", "cis", "trans", "stron, mon"
), rep3 = c("na", "na", "bay", "eng", "giad", "china", "sing", 
"giad", "china", "china", "china, camp", "camp", "na", "na", 
"cis", "trans", "stron, mon")), row.names = c(NA, -17L), class = c("data.table", 
"data.frame"))

And here is what I want:

    want <- structure(list(rep1 = c("china", "na", "bay", "eng", "giad", 
"china", "sing", "giad", "na", "china", "china, camp", "guat,camp", 
"na", "na", "cis", "trans", "stron, mon"), rep2 = c("china", 
"na", "bay", "eng", "giad", "china", "sing", "giad", "na", "china", 
"china, camp", "camp", "na", "na", "cis", "trans", "stron, mon"
), rep3 = c("na", "na", "bay", "eng", "giad", "china", "sing", 
"giad", "china", "china", "china, camp", "camp", "na", "na", 
"cis", "trans", "stron, mon"), rep1_chi = c(1, 0, 0, 0, 0, 1, 
0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0), rep2_chi = c(1, 0, 0, 0, 0, 
1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0), rep3_chi = c(0, 0, 0, 0, 
0, 1, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0), rep1_bay = c(0, 0, 1, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), rep2_bay = c(0, 0, 
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), rep3_bay = c(0, 
0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), rep1_gia = c(0, 
0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0), rep2_gia = c(0, 
0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0), rep3_gia = c(0, 
0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0), rep1_sin = c(0, 
0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), rep2_sin = c(0, 
0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), rep3_sin = c(0, 
0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)), class = "data.frame", row.names = c(NA, 
-17L))

I was able to create a working solution using ifelse and stringr::str_detect as follows:

want <- have %>% dplyr::select(rep1, rep2, rep3) %>% mutate(
      rep1_chi = ifelse(str_detect(rep1,"chi") == T,1,0),
      rep2_chi = ifelse(str_detect(rep2,"chi") == T,1,0),
      rep3_chi = ifelse(str_detect(rep3,"chi") == T,1,0),
      rep1_bay = ifelse(str_detect(rep1,"bay") == T,1,0),
      rep2_bay = ifelse(str_detect(rep2,"bay") == T,1,0),
      rep3_bay = ifelse(str_detect(rep3,"bay") == T,1,0),          
      rep1_gia = ifelse(str_detect(rep1,"gia") == T,1,0),
      rep2_gia = ifelse(str_detect(rep2,"gia") == T,1,0),
      rep3_gia = ifelse(str_detect(rep3,"gia") == T,1,0),           
      rep1_sin = ifelse(str_detect(rep1,"sin") == T,1,0),
      rep2_sin = ifelse(str_detect(rep2,"sin") == T,1,0),
      rep3_sin = ifelse(str_detect(rep3,"sin") == T,1,0))

My biggest issue is that it seems rather repetitive. I was wondering if there is a more elegant solution? Considering that the "rep" columns are numerically ordered 1-3 I thought there might be a better way to program this.

Looking through SO I found the following solution using model.matrix that seems to work nicely when you want every pattern and are only interested in a single column. I tried turning this into a function so I can select multiple columns - but I would still have to delete the strings with the patterns that are not of interest.

daszlosek
  • 1,366
  • 10
  • 19

2 Answers2

2

Here's an approach using mutate_all. If you wanted to do this to only specific columns, you would just use mutate_at and specify the columns instead.

library(dplyr)
library(stringr)

mutate_all(have, funs(chi = as.numeric(str_detect(., "chi")),
                  bay = as.numeric(str_detect(., "bay")),
                  gia = as.numeric(str_detect(., "gia")),
                  sin = as.numeric(str_detect(., "sin"))))

mutate_at example with vars:

want <- have %>% mutate_at(vars(rep1,rep2,rep3), funs( 
                           tox = as.numeric(str_detect(., "chi")), 
                           bay = as.numeric(str_detect(., "bay")), 
                           gia = as.numeric(str_detect(., "gia")), 
                           iso = as.numeric(str_detect(., "sin"))))
Jake Kaupp
  • 7,892
  • 2
  • 26
  • 36
1

Here is some ugly and inefficient (performance wise) base code where you don't have to construct the colnames yourself:

want_new <- have
colold <- colnames(want_new)
for (p in pattern) {
  cname <- paste0(
    colold, 
    "_",
    p
  )
  for (col in cname) {
    want_new[, col] <- as.numeric(str_detect(
      want_new[, gsub(paste0("_", p), "", col, fixed)],
      p
    ))
  }
}

Pretty sure this can be improved with a bit further tweaking.

JBGruber
  • 11,727
  • 1
  • 23
  • 45