user7446890
user7446890

Reputation: 121

Using case_when for conditional statement with dplyr across

I'm trying to do a case_when between several columns with similar names using dplyr across:

For instance: Dataframe

df <-
  data.frame(f1_nom = c(1,2,5,7,2,3,5,1), f2_nom = c(3,4,2,8,3,4,3,7),
             f3_nom = c(1,2,5,7,5,8,5,1), f4_nom = c(3,4,2,8,3,3,3,2),
             f5_nom = c(2,3,5,1,6,8,5,9), f6_nom = c(3,4,3,7,5,7,5,8), 
             f7 = c(1,2,4,7),
             f1_nom_wt = c(1,3,1,2,7,2,3,5), f2_nom_wt = c(6,8,5,9,4,2,8,3),
             f3_nom_wt = c(1,1,1,2,5,7,5,8), f4_nom_wt = c(3,3,3,2,1,6,8,5),
             f5_nom_wt = c(5,8,5,1,6,8,5,9), f6_nom_wt = c(6,11,1,2,5,9,4,2))

what am trying to do is:

mutate(case_when(f1_nom <= f1_nom_wt ~ 1, TRUE~  0))

I have hundred of these, but observe that the the names are similar except for the "_wt".

my attempt:

res1 <- DF1 %>% 
  mutate(across(f1_nom:f100_nom, ~ case_when(.x <= "{.col}_wt" ~ 1, TRUE~ 0),.names = "{.col}_xyz"))

I was thinking by concat the name I could get the values for all the _wt for the case, but my results are all zeros. I know am doing this wrong. Basically i want the case to compare between

f1_nom:f100_nom and f1_nom_wt:f100_nom_wt

Thanks for your help!

Upvotes: 0

Views: 750

Answers (1)

Ronak Shah
Ronak Shah

Reputation: 389225

Couple of options -

  1. Using tidyverse -
library(dplyr)
library(purrr)

df %>%
  mutate(map2_df(across(ends_with('_nom'), .names = '{col}_val'), 
                 across(ends_with('_wt')), ~as.integer(.x <= .y)))

#  f1_nom f2_nom f3_nom f4_nom f5_nom f6_nom f7 f1_nom_wt f2_nom_wt f3_nom_wt f4_nom_wt
#1      1      3      1      3      2      3  1         1         6         1         3
#2      2      4      2      4      3      4  2         3         8         1         3
#3      5      2      5      2      5      3  4         1         5         1         3
#4      7      8      7      8      1      7  7         2         9         2         2
#5      2      3      5      3      6      5  1         7         4         5         1
#6      3      4      8      3      8      7  2         2         2         7         6
#7      5      3      5      3      5      5  4         3         8         5         8
#8      1      7      1      2      9      8  7         5         3         8         5

#  f5_nom_wt f6_nom_wt f1_nom_val f2_nom_val f3_nom_val f4_nom_val f5_nom_val f6_nom_val
#1         5         6          1          1          1          1          1          1
#2         8        11          1          1          0          0          1          1
#3         5         1          0          1          0          1          1          0
#4         1         2          0          1          0          0          1          0
#5         6         5          1          1          1          0          1          1
#6         8         9          0          0          0          1          1          1
#7         5         4          0          1          1          1          1          0
#8         9         2          1          0          1          1          1          0
  1. Base R -
wt_cols <- grep('_wt', names(df), value = TRUE)
cols <- sub('_wt', '', wt_cols)
df[paste0(cols, '_val')] <- +(df[cols] <= df[wt_cols])
df

Upvotes: 1

Related Questions