0

DataFrame, "data1", has column names "standard", "voffset", "temp", "loop_div". I am able to create a new column, "legend", using mutate:

data2=mutate(data1,legend = paste(standard,voffset,temp,loop_div, sep="_"))

However, I would like to create a variable to contain the column names to use in mutate, instead, because the columns to use will change depending on the dataframe I am using. I have tried using a single quoted string as follows:

legend_cols='standard,voffset,temp,loop_div' data2=mutate(data1,legend = paste(legend_cols, sep="_"))

This produces "standard,voffset,temp,loop_div" for every value in the legend column of the dataframe, data2$legend. This is because I've lost the reference to the actual columns.

How can I create column references in my legend_cols string variable, or is there a better approach to mutate with a variable set of columns?

DCD
  • 1
  • 1
  • You should read the `dplyr` vignetted [on non-standard evaluation](https://cran.r-project.org/web/packages/dplyr/vignettes/nse.html). – Gregor Thomas Jul 05 '16 at 23:23
  • `DF %>% mutate(legend = paste(names(.), collapse = "_"))` creates a new column named `legend` in which every row has the concatenated names of the columns of `DF`. – G. Grothendieck Jul 05 '16 at 23:26

0 Answers0