I have written a function that gathers some data from wide to long format. I need to this this for muliple times and therefore wanted to write a function that does it. Here is my function:
longFormat <- function(df, var){
dflong <- df %>%
gather(key = CONDITION, value = var, contains(var))
dflong
}
if I pass the argument 'coltype_a' like so:
newdf <- longFormat(dfwide, 'coltype_a')
then the resulting dataframe is correct in that the fuction has looked for all columns that contain 'coltype_a' in the column name amd has turned them to long format. However, I wanted the function to name the value column as 'coltype_a' but instead the columns are being named as 'var'. Does anyone know how to get dplyr to take the argument that I give to the function LongFormat and assign it to value in the gather function?
Many thanks!