I thought of simplifying my code a bit and tried to engage with the world of custom build functions. I build the following dummy:
library(dplyr, tidyverse)
##
string <- c("car", "train", 'bike', 'plain')
speed1 <- runif(4, min = 0, max = 10000)
speed2 <- runif(4, min = 0, max = 10000)
n1 <- sample(1:100, 4)
n2 <- sample(1:100, 4)
##
df <- data.frame(string, speed1, speed2, n1, n2)
df
Basically, I want to do simple operations of the following type:
df <- df%>%
dplyr::mutate(speed1_n1 = paste0(format(speed1, big.mark = ",") , '\n(' , format(n1, big.mark = ",") , ')'))
df
I can build a function:
my_fun <- function(dataf, V1, V2){
dataf <- dataf%>%
dplyr::mutate(speed1_n1 = paste0(format(V1, big.mark = ",") , '\n(' , format(V2, big.mark = ",") , ')'))}
df<-df%>%my_fun( df$speed1, df$n1)
df
However, the variable names needs to be generated dynamically from the called variable names similar to the question here, but within a function. I cannot manually specify mutate(speed1_n1=...
. Something, like: !!paste('speed1', 'n1', sep = "_"):=
, but with the variable / column names and not in quotes.