Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to add a third dummy variable using ifelse() in R?

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.

like image 516
Adri Avatar asked Aug 11 '17 19:08

Adri


2 Answers

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.*)", "")
like image 173
Gregor Thomas Avatar answered Oct 19 '22 22:10

Gregor Thomas


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)
like image 25
www Avatar answered Oct 19 '22 22:10

www