I am having difficulties with utilizing a dynamic variable in dplyr (specifically, using dply::mutate to pick out a specific column). It seems there are quite a few posts online about using dynamic variables in dplyr, but I've gotten confused by this. It seems the solution to use a dynamic variable, you need to write a function, then apply this to your dplyr pipe.
Is there a way to directly use a dynamic variable in a dplyr::mutate, either as a new column name and/or to reference a column that we want to apply the calculation to?
Below is an example (where I am trying to use two variables; newcol as the name of the new column I'm trying to create, and col as the name of the column I'm trying to apply the calculation to).
#libraries
library(dplyr)
#make dataframe and variables for name
testdf <- data.frame(cola=c(1,2,3),colb=c(4,5,6),colc=c(7,8,9))
col <- "cola"
newcol <- "newcolname"
#mutate: new column (with variable column name & variable column which I'm using to calculate)
testdf %>%
dplyr::mutate(newcol=col*5)
Thanks for your help!