I have created this custom function with the help of @jared_mamrot Make a custom function of an dplyr procedure
It basically takes a dataframe, a column and a number as argument and replaces in that column a defined percent (y) of values with NA's:
my_func <- function(df,x,y){
df %>%
mutate({{x}} := replace({{x}}, sample(row_number(),
size = ceiling(y * n()), replace = FALSE), NA))
}
Now I would like to apply this function to multiple columns using mutate(across...
My try so far:
mtcars %>%
mutate(across(1:3, ~my_func(mtcars, ., 0.3)))
This does essentially what the function should do but the whole dataframe is repeated x times.
What I want is:
The function should only be applied to column 1:3.
Adding the .names =
argument does not solve the issue.
So I guess I have to modify the function?