Pass variable to tidyr's gather to rename key/value columns?

To put it in a function you have to use gather_() like so.

myFunc <- function(mydata, key.col, val.col, gather.cols) {
  new.data <- gather_(data = mydata,
                      key_col = key.col,
                      value_col = val.col,
                      gather_cols = colnames(mydata)[gather.cols])
  return(new.data)    
}

temp.data <- data.frame(day.1 = c(20, 22, 23), day.2 = c(32, 22, 45),
day.3 = c(17, 9, 33))
temp.data


     day.1 day.2 day.3
1    20    32    17
2    22    22     9
3    23    45    33

# Call my custom function, renaming the key and value columns 
# "day" and "temp", respectively

long.data <- myFunc(mydata = temp.data, key.col = "day", val.col =   
"temp", gather.cols = 1:3)
# Columns *have* been renamed as desired
head(long.data)

  day temp
1 day.1   20
2 day.1   22
3 day.1   23
4 day.2   32
5 day.2   22
6 day.2   45

As stated, the main difference is in gather_ you have to specify the columns you want to gather up with the gather_cols argument.


...and having had the same question, I now found the answer here: https://dplyr.tidyverse.org/articles/programming.html

You can have dplyr evaluate symbols by setting them off with exclamation marks. In your original question, the code would be:

gather(data = mydata, key = !!key.col value = !!val.col)

Tags:

R

Tidyr