Comparing gather (tidyr) to melt (reshape2)

Solution 1:

Your gather line should look like:

dat %>% gather(variable, date, -teacher, -pd)

This says "Gather all variables except teacher and pd, calling the new key column 'variable' and the new value column 'date'."


As an explanation, note the following from the help(gather) page:

 ...: Specification of columns to gather. Use bare variable names.
      Select all variables between x and z with ‘x:z’, exclude y
      with ‘-y’. For more options, see the select documentation.

Since this is an ellipsis, the specification of columns to gather is given as separate (bare name) arguments. We wish to gather all columns except teacher and pd, so we use -.

Solution 2:

In tidyr 1.0.0 this task is accomplished with the more flexible pivot_longer().

The equivalent syntax would be

library(tidyr)
dat %>% pivot_longer(cols = -c(teacher, pd), names_to = "variable", values_to = "date")

which says, correspondingly, "pivot everything longer except teacher and pd, calling the new variable column "variable" and the new value column "date".

Note that the long data comes back in order firstly of the columns of the previous data frame that were pivoted, unlike from gather, which came back in the order of the new variable column. To rearrange the resultant tibble, use dplyr::arrange().