5

I was using this code to create a new Group column based on partial strings found inside the column var for 2 groups, Sui and Swe. I had to add another group, TRD, and I've been trying to tweak the ifelse function do this, but no success. Is this doable? are there any other solutions or other functions that might help me do this?

m.df <- molten.df%>% mutate(
Group = ifelse(str_detect(variable, "Sui"), "Sui", "Swedish"))

Current m.df: 
                          var      value    
  ADHD_iFullSuiTrim.Threshold1 0.00549427     
  ADHD_iFullSuiTrim.Threshold1 0.00513955     
  ADHD_iFullSweTrim.Threshold1 0.00466352   
  ADHD_iFullSweTrim.Threshold1 0.00491633   
  ADHD_iFullTRDTrim.Threshold1 0.00658535    
  ADHD_iFullTRDTrim.Threshold1 0.00609122    


Desired Result:
                          var      value    Group
   ADHD_iFullSuiTrim.Threshold1 0.00549427    Sui  
   ADHD_iFullSuiTrim.Threshold1 0.00513955    Sui  
   ADHD_iFullSweTrim.Threshold1 0.00466352   Swedish
   ADHD_iFullSweTrim.Threshold1 0.00491633   Swedish
   ADHD_iFullTRDTrim.Threshold1 0.00658535    TRD
   ADHD_iFullTRDTrim.Threshold1 0.00609122    TRD  

Any help or suggestion would be appreciated even if the result can be accomplished using other functions.

Adri
  • 121
  • 8

3 Answers3

7

No ifelse() is needed. I'd use Group = str_extract(var, pattern = "(Sui)|(TRD)|(Swe)").

You could do fancier regex with a lookbehind for "iFull" and a lookahead for "Trim", but I can never remember how to do that.

A little more roundabout, but general if you want whatever is between "iFull" and "Trim" would be a replacement:

str_replace_all(var, pattern = "(.*iFull)|(Trim.*)", "")
Gregor Thomas
  • 136,190
  • 20
  • 167
  • 294
6

Try to use multiple ifelse

library(dplyr)
library(stringr)

m.df <- molten.df %>% 
  mutate(Group = ifelse(str_detect(var, "Sui"), "Sui", 
                        ifelse(str_detect(var, "Swe"), "Swedish", "TRD")))

Or case_when

m.df <- molten.df %>% 
  mutate(Group = case_when(
    str_detect(var, "Sui") ~ "Sui",
    str_detect(var, "Swe") ~ "Swe",
    TRUE                   ~ "TRD"
  ))

Data Preparation

molten.df <- read.table(text = "var      value    
  'ADHD_iFullSuiTrim.Threshold1' 0.00549427     
                 'ADHD_iFullSuiTrim.Threshold1' 0.00513955     
                 'ADHD_iFullSweTrim.Threshold1' 0.00466352   
                 'ADHD_iFullSweTrim.Threshold1' 0.00491633   
                 'ADHD_iFullTRDTrim.Threshold1' 0.00658535    
                 'ADHD_iFullTRDTrim.Threshold1' 0.00609122",
                header = TRUE, stringsAsFactors = FALSE)
www
  • 38,575
  • 12
  • 48
  • 84
5

For future reference - provide all the necessary components for repeating the analysis e.g., packages and example data

# load ----
library(dplyr)
library(stringr)

# data ----
 df=data.frame(var=c('ADHD_iFullSuiTrim.Threshold1',
  'ADHD_iFullSuiTrim.Threshold1',
  'ADHD_iFullSweTrim.Threshold1',
  'ADHD_iFullSweTrim.Threshold1',
  'ADHD_iFullTRDTrim.Threshold1', 
  'ADHD_iFullTRDTrim.Threshold1'),
   value = c(0.00549427, 0.00513955, 0.00466352, 0.00491633, 0.00658535, 0.00609122))    

df %>% 
    mutate(Group = case_when(str_detect(var, "Sui")~"Sui",
                                 str_detect(var, "Swe")~"Swedish",
                                 str_detect(var, "TRD")~"TRD"))
B Williams
  • 1,992
  • 12
  • 19
  • Thank you. This is helpful I'm new on here and do not know very well how to format my questions. – Adri Aug 11 '17 at 19:53