3
votes

Given a data frame df like below

text <- "
model,var,value
M1,a,12211
M1,b1,10.21
M1,b2,5.07
M1,c1,41.8
M1,c2,58.2
M1,d,1.6
M2,a,11922
M2,b1,15.6
M2,b2,8.9
M2,c1,38.1
M2,c2,61.9
M2,d,1.8
M2,a,13101
M2,b1,9.21
M2,b2,6.56
M2,c1,36.07
M2,c2,63.93
M2,d,1.75
"
df <- read.table(textConnection(text), sep=",", header = T)

I want to add a col var2 based on the value of var via dplyr mutate.

Based on the following logic.

If var == 'b1' or var == 'b2' then All B If var == 'c1' or var == 'c2' then All C else var

I want to store the mapping as follows and use it to build the above logic

mapping <- c("All B"= list(c('b1', 'b2')), "All C" = list(c('c1', 'c2')))
> mapping    
$`All B`
[1] "b1" "b2"

$`All C`
[1] "c1" "c2"

The expected output is

   model var    value var2
1     M1   a 12211.00    a
2     M1  b1    10.21    All B
3     M1  b2     5.07    All B
4     M1  c1    41.80    All C
5     M1  c2    58.20    All C
6     M1   d     1.60    d
7     M2   a 11922.00    a
8     M2  b1    15.60    All B
9     M2  b2     8.90    All B
10    M2  c1    38.10    All C
11    M2  c2    61.90    All C
12    M2   d     1.80    d
13    M2   a 13101.00    a
14    M2  b1     9.21    All B
15    M2  b2     6.56    All B
16    M2  c1    36.07    All C
17    M2  c2    63.93    All C
18    M2   d     1.75    d

I plan to use dplyr with an ifelse as follows

df %>%
  mutate(var2 = ifelse(# what should go here )
5
could you also suggest how to use the mapping list included to dynamically pick up the var2 valuesuser3206440
@eipi10 I get the following error with your code for case_when: "Error: 'match' requires vector arguments". Have you seen this before ? Also, you are missing a trailing ")" for the mutate function.steveb

5 Answers

2
votes

Here is an example solution using case_when (as suggested in the comments):

df %>%
    mutate(
        var = as.character(var),
        var2 = case_when(
            var == "b1" | var == "b2" ~ "All B",
            var == "c1" | var == "c2" ~ "All C",
            TRUE ~ var))
#   model var    value  var2
#1     M1   a 12211.00     a
#2     M1  b1    10.21 All B
#3     M1  b2     5.07 All B
#4     M1  c1    41.80 All C
#5     M1  c2    58.20 All C
#6     M1   d     1.60     d
#7     M2   a 11922.00     a
#8     M2  b1    15.60 All B
#9     M2  b2     8.90 All B
#10    M2  c1    38.10 All C
#11    M2  c2    61.90 All C
#12    M2   d     1.80     d
#13    M2   a 13101.00     a
#14    M2  b1     9.21 All B
#15    M2  b2     6.56 All B
#16    M2  c1    36.07 All C
#17    M2  c2    63.93 All C
#18    M2   d     1.75     d    
2
votes

We can create a function which returns the list name if var exist in mapping else it returns var. We can use rowwise() to execute this function for each row.

get_right_mapping <- function(var) {
   names(which(sapply(mapping, function(x) var %in% x)))
 }

library(dplyr)
df %>%
   rowwise() %>%
   mutate(var2 = ifelse(var %in% unlist(mapping), get_right_mapping(var), var))


#   model var      value  var2 
#   <fct> <chr>    <dbl> <chr>
# 1 M1    a        12211    a    
# 2 M1    b1       10.2  All B
# 3 M1    b2        5.07 All B
# 4 M1    c1       41.8  All C
# 5 M1    c2       58.2  All C
# 6 M1    d        1.60     d    
# 7 M2    a        11922    a    
# 8 M2    b1       15.6  All B
# 9 M2    b2        8.90 All B
#10 M2    c1       38.1  All C
#11 M2    c2       61.9  All C
#12 M2    d         1.80 d    
#13 M2    a         13101    a    
#14 M2    b1        9.21 All B
#15 M2    b2        6.56 All B
#16 M2    c1       36.1  All C
#17 M2    c2       63.9  All C
#18 M2    d         1.75 d    

data

mapping <- c( "All A"= list(c('a1', 'a2')), "All B" = list(c('b1', 'b2')), 
              "All C" = list(c('c1','c2')))
df$var <- as.character(df$var)
2
votes

As an aside, there is a little-used levels<- function in base R that does something near identical to the required result. If you pass a named list on the right side, each value in the list is replaced by the name. So, if retaining the mapping as a source is important, try:

mapping <- c("B"= list(c('b1', 'b2')), "C" = list(c('c1', 'c2')))

df$var2 <- df$var
othval  <- setdiff(df$var, unlist(mapping))
levels(df$var2) <- c(mapping, setNames(othval,othval))
# [1] a B B C C d a B B C C d a B B C C d
#Levels: B C a d

(Most of the stuffing about here is accounting for the cases that are not covered by the mapping)

0
votes

I've simplified your logic - correct me if I'm wrong.

If var = b or c followed by digits, substitute All B or All C, respectively.

df %>% 
  mutate(var2 = gsub("^([bc])\\d+", 
                     paste("All", "\\U\\1"), 
                     var, 
                     perl = TRUE))

   model var    value  var2
1     M1   a 12211.00     a
2     M1  b1    10.21 All B
3     M1  b2     5.07 All B
4     M1  c1    41.80 All C
5     M1  c2    58.20 All C
6     M1   d     1.60     d
7     M2   a 11922.00     a
8     M2  b1    15.60 All B
9     M2  b2     8.90 All B
10    M2  c1    38.10 All C
11    M2  c2    61.90 All C
12    M2   d     1.80     d
13    M2   a 13101.00     a
14    M2  b1     9.21 All B
15    M2  b2     6.56 All B
16    M2  c1    36.07 All C
17    M2  c2    63.93 All C
18    M2   d     1.75     d
0
votes

If you convert your mapping as a data.frame and gather the values you're a right join away from your goal.

library(tidyverse)
as_tibble(mapping) %>% rename_all(~paste0("All_",.x)) %>%
  gather(var2,var) %>% right_join(df) %>%   # main chunk
  mutate(var2=ifelse(is.na(var2),var,var2)) # replace nas

# # A tibble: 18 x 4
#     var2   var  model    value
#    <chr> <chr> <fctr>    <dbl>
#  1     a     a     M1 12211.00
#  2 All_B    b1     M1    10.21
#  3 All_B    b2     M1     5.07
#  4 All_C    c1     M1    41.80
#  5 All_C    c2     M1    58.20
#  6     d     d     M1     1.60
#  7     a     a     M2 11922.00
#  8 All_B    b1     M2    15.60
#  9 All_B    b2     M2     8.90
# 10 All_C    c1     M2    38.10
# 11 All_C    c2     M2    61.90
# 12     d     d     M2     1.80
# 13     a     a     M2 13101.00
# 14 All_B    b1     M2     9.21
# 15 All_B    b2     M2     6.56
# 16 All_C    c1     M2    36.07
# 17 All_C    c2     M2    63.93
# 18     d     d     M2     1.75