When I create transformed variables in a dataframe (e.g., standardized versions of existing variables), it would often be convenient if they were placed alongside their "parent" variables in this manner (e.g., varname1, varname1_z, varname2, varname2_z, etc.). But the default placement of new variables is at the end of the dataframe.
Is there a way to efficiently place newly created variables alongside their "parent" variable, so as to keep the data better organized?
I can currently change variable positions in the dataframe using select() and a custom function called moveme(), but I am trying to make this variable placement process more automated, so that variables are placed as they are created.
In the code example below you'll see all of my newly created "_z" variables at the end of the dataframe. Is there a way I could place them alongside their unstandardized parent in an automated manner?
Thank you for any support.
library(tidyverse)
mpg %>%
mutate_if(is.numeric, funs(z = scale(.) %>% as.double()))