0

I have the following code

library(dplyr)
p <- data.frame(a = rep(0:2, times = 5)) %>%
       mutate(a1 = a == 1,a2 = a == 2)

I want to put the expressions in mutatein a function (or a list, I do not really care) so that I define them before running mutate

I tried this:

mutatemy=function(){
  (a1=a==1,a2=a==2)
}

p <- data.frame(a = rep(0:2, times = 5)) %>%
mutate(mutatemy())

which does not work of course.

Ideally, I should be able to flexibly put all possible mutate combinations such as the use of across.

How could I achieve this.

The background is that I mutate inside a function and I want the user to allow specifying exactly what to mutate without manipulating the function.

Here is a simplified example of what I want to eventually achieve.


mainfunction <- function(data=data){

#somecode...

data2<- data %>% mutate(r=a*a, # hardcoded 
                        userinput) # here I want to allow the user to decide what else to mutate
return(data2)
}

## user supplies data
data= data.frame(data.frame(a=rep(0:2,times=5))

## and the desired manipulations based on the supplied dataset
userinput <- (a1=a==1)

## and then applies this function
mainfunction(data)

#somecode...

Thanks a lot! Julian

Mark
  • 7,785
  • 2
  • 14
  • 34
  • 4
    There are alot of things wrong with this. First, why would you want to hard code outside mutate? If at all you have changing variables then you rinterest should be how to program with mutate. – Onyambu Jul 27 '23 at 23:57
  • This works ```expr2 <- list(a1 = expr( a==1), a2= expr( a==2)) p=data.frame(a=rep(0:2,times=5)) %>% mutate(!!! expr2) ``` But it still does not allow me to use `across` – Julian Sagebiel Jul 27 '23 at 23:59
  • 2
    Still the question is why would you want to hard code outside mutate? WHat are you trying to solve? – Onyambu Jul 28 '23 at 00:01
  • sorry, I tried to explain in the edited question now. – Julian Sagebiel Jul 28 '23 at 00:21
  • 3
    Hi Julian! It might be helpful to come up with a real toy example of what you want. I can think of quite a few different answers which might be the one you want, but it's unclear right now which one that might be. @Onyambu is right, this is quite odd – Mark Jul 28 '23 at 02:42
  • 1
    In your example with the list of expressions, the only input column `a` is hard-coded - it's not at all clear what `across()` would do here, as `across` is generally used select inputs. I strongly agree with the other commenters that this example doesn't seem to illustrate your problem well. – Gregor Thomas Jul 28 '23 at 03:52
  • @GregorThomas: You can also use it to make many variables at the same time, e.g. `across(.cols=1:3,.fns = ~ rgumbel(setpp,loc=0, scale=1), .names = "{'e'}_{1:3}" )` and this is what I want the user to specify. Additionally to the fixed procedures the data processing in the function is doing, the user can manipulate variables. – Julian Sagebiel Jul 28 '23 at 13:01
  • If you want the user to be able to pass though addition transformations, consider passing them along with `...`. That's described at https://dplyr.tidyverse.org/articles/programming.html – MrFlick Jul 28 '23 at 13:04
  • @JulianSagebiel Yes, you can use `across` to make many variables at once. But your `expr2` example doesn't do that either... it hardcodes both the columns tested and resulting column names. Can you show an example with `across` where `across` does something? – Gregor Thomas Jul 28 '23 at 14:11
  • I want to give a completely new example and explanation for my problem, but I am not sure what the correct behavior is. Shall I just edit my initial question (which means it will be to 80% new) or shall I post the problem in a new answer or new thread? Sorry for this noop question. – Julian Sagebiel Aug 16 '23 at 22:28

0 Answers0