Use paste/paste0 for the "key" argument in gather()

Viewed 31

I created a toy data frame to demonstrate my question:

a = 0
df = data.frame(x = 1:2, y = 2:3, z = 3:4)
df %>% gather(key = "name0", value = "val", -1) # this works fine
df %>% gather(key = paste0("name", a), value = "val", -1) # but this gives an error

The error message was

Error in `ensym2()`:
! Must supply a symbol or a string as argument

I hope to specify the "key" column's name as name0 but 0 can be flexibly changed by paste0(). I have done the searching but couldn't find a solution!

Thanks in advance!

0 Answers
Related