Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Use Dplyr::Select Functions within Purrr:Map2 All Within a Pipe

Tags:

r

tidyverse

library(tidyverse)

Using the example data (bottom), I'm attempting something like the code below, but I'm stuck on how to use this strategy of selecting columns by name using dplyr::select functions within map2. This particular example uses regex, but I want an approach that could also use "contains", or "ends_with" or "starts_with" from dplyr::select.

I realize I could use indexes, below...which works...

map2(Df[8:12],Df[3:7],~ if_else(.x != 3, recode(.y,`1`=0,`2`=0,`3`=0,`4`=1,`5`=1),88)) %>%
as.data.frame %>%
rename_all(paste0,"_new") %>%
cbind(Df,.)

But since my real dataset has many column names I just want to use "select" functions to name them, or use regex. I've tried variations of the code below, but none seem to work. How can I correct this?

The code below first subsets since I only want the variables I'm working with, then pipes into map2.

Df<-Df%>%select(Code,Pet,matches("^q.*s$"),matches("^q.*i$"))%>%
map2(Df(matches("^q.*i$")), Df(matches("^q.*s$")), ~ if_else(.x != 1,  
recode(.y,`1`=0,`2`=0,`3`=0,`4`=1,`5`=1),88)) %>%
as.data.frame %>%
rename_all(paste0,"_new") %>%
cbind(Df,.)

Example Data:

q25i<-c(2,1,88,2,1,2,2,2)
q26i<-c(2,88,88,88,2,2,2,1)
q27i<-c(2,2,1,1,1,1,1,2)
q28i<-c(88,1,1,2,2,2,2,88)
q29i<-c(1,1,1,2,2,1,88,2)
q25s<-c(3,5,88,4,1,4,4,5)
q26s<-c(4,4,5,5,1,4,4,3)
q27s<-c(3,3,4,1,4,5,5,3)
q28s<-c(4,5,88,1,3,2,2,2)
q29s<-c(88,88,3,4,4,3,3,2)
q25U<-c(2,4,4,4,4,4,5,4)
q26U<-c(5,4,6,5,4,3,6,7)
q27U<-c(4,3,2,3,3,3,2,1)
q28U<-c(4,3,2,3,3,2,3,1)
q29U<-c(4,3,5,5,4,3,3,2)
Code<-c("P1","AB","AB","P1","P1","CD","AAA","CD")
Pet<-c("Dog","Cat","Dog","Fish","Dog","Cat","Rabbit","Fish")

 Df<-data.frame (Code,Pet,q25U,q26U,q27U,q28U,q29U,q25i,q26i,q27i,q28i,q29i,q25s,q26s,q27s,q28s,q29s)
like image 323
Mike Avatar asked Feb 02 '26 08:02

Mike


1 Answers

Perhaps this is what you're looking for:

library(dplyr)
library(purrr)

list(select(Df, matches("^q.*i$")), select(Df, matches("^q.*s$"))) %>%
  pmap( ~ if_else(.x != 1, recode(.y,`1`=0,`2`=0,`3`=0,`4`=1,`5`=1),88)) %>%
  as.data.frame %>%
  rename_all(paste0,"_new") %>%
  cbind(Df,.)

Result:

  Code    Pet q25U q26U q27U q28U q29U q25i q26i q27i q28i q29i q25s q26s q27s q28s q29s
1   P1    Dog    2    5    4    4    4    2    2    2   88    1    3    4    3    4   88
2   AB    Cat    4    4    3    3    3    1   88    2    1    1    5    4    3    5   88
3   AB    Dog    4    6    2    2    5   88   88    1    1    1   88    5    4   88    3
4   P1   Fish    4    5    3    3    5    2   88    1    2    2    4    5    1    1    4
5   P1    Dog    4    4    3    3    4    1    2    1    2    2    1    1    4    3    4
6   CD    Cat    4    3    3    2    3    2    2    1    2    1    4    4    5    2    3
7  AAA Rabbit    5    6    2    3    3    2    2    1    2   88    4    4    5    2    3
8   CD   Fish    4    7    1    1    2    2    1    2   88    2    5    3    3    2    2
  q25i_new q26i_new q27i_new q28i_new q29i_new
1        0        1        0        1       88
2       88        1        0       88       88
3       88        1       88       88       88
4        1        1       88        0        1
5       88        0       88        0        1
6        1        1       88        0       88
7        1        1       88        0        0
8        1       88        0        0        0

Notes:

This matches with your result if you use the correct indices:

map2(Df[8:12],Df[13:17],~ if_else(.x != 1, recode(.y,`1`=0,`2`=0,`3`=0,`4`=1,`5`=1),88)) %>%
  as.data.frame %>%
  rename_all(paste0,"_new") %>%
  cbind(Df,.)

The reason for using pmap instead of map2 is because pmap takes a list of inputs whereas map2 takes exactly two inputs. For instance, the following uses map2 instead of pmap:

list(select(Df, matches("^q.*i$")), select(Df, matches("^q.*s$"))) %>%
  {map2(.[[1]], .[[2]], ~ if_else(.x != 1, recode(.y,`1`=0,`2`=0,`3`=0,`4`=1,`5`=1),88))} %>%
  as.data.frame %>%
  rename_all(paste0,"_new") %>%
  cbind(Df,.)

This is less convenient, IMO, since you have to specify the inputs manually and you have to wrap map2 with {} to override the %>% default of piping into the first argument.

like image 156
acylam Avatar answered Feb 03 '26 22:02

acylam



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!