I would like to use a function in order to calculate a new variable based on existing variables within a dataframe. The name of the new variable should be defined as a function argument: e.g. function(df, varname = "myVarname"):
calculateTime <- function(df, varname){
df <- df %>% mutate(varname = end - begin )
return(df)
}
calculateTime(df, "myVarname")
Unfortunately, this code doesn't work.
Can anyone help me?