[Solved] Ifelse to Compare the content of columns with dplyr


The best way to do this would be to use the glue package. This allows you to easily assign variable names based on strings:

library(tidyverse)
library(glue)

> df <- data.frame(
    sample_id = c('SB013', 'SB014', 'SB013', 'SB014', 'SB015', 'SB016', 'SB016'), 
    IN = c(1,2,3,4,5,6,7), 
    OUT = c(rep('out',7)))

> df
  sample_id IN OUT
1     SB013  1 out
2     SB014  2 out
3     SB013  3 out
4     SB014  4 out
5     SB015  5 out
6     SB016  6 out
7     SB016  7 out

> df %>% mutate(`SB013` = ifelse(sample_id == 'SB013', IN, OUT))

  sample_id IN OUT SB013
1     SB013  1 out     1
2     SB014  2 out   out
3     SB013  3 out     3
4     SB014  4 out   out
5     SB015  5 out   out
6     SB016  6 out   out
7     SB016  7 out   out

Note that in this case, the dplyr command if_else won’t work because IN and OUT are of different types. If it is a large dataset, I would suggest using a function with vectorisation (map or lapply), but if there are only 4 unique sample_id:

> list_of_ids <- 
    df %>% 
    distinct(sample_id) %>%
    unlist() %>%
    unname()

> list_of_ids
[1] "SB013" "SB014" "SB015" "SB016"


> for(current_id in list_of_ids){
df <- 
    df %>% 
    mutate('{current_id}' := ifelse(sample_id == current_id, IN, OUT))
}

> df
  sample_id IN OUT SB013 SB014 SB015 SB016
1     SB013  1 out     1   out   out   out
2     SB014  2 out   out     2   out   out
3     SB013  3 out     3   out   out   out
4     SB014  4 out   out     4   out   out
5     SB015  5 out   out   out     5   out
6     SB016  6 out   out   out   out     6
7     SB016  7 out   out   out   out     7

In future, please add sample data and expected output to your question.

solved Ifelse to Compare the content of columns with dplyr