Reputation: 11480
I know there is gather_()
but its not working with matches
and so ...
dynamicKey = "coolKey"
mtcars[,8:11] %>% gather(dynamicKey,values,matches("vs|am"))
gear carb dynamicKey values
1 4 4 vs 0
2 4 4 vs 0
3 4 1 vs 1
4 3 1 vs 1
5 3 2 vs 0
6 3 1 vs 1
...
gear carb coolKey values
1 4 4 vs 0
2 4 4 vs 0
3 4 1 vs 1
4 3 1 vs 1
5 3 2 vs 0
6 3 1 vs 1
...
google, SO, get(dynamicKey)
... nothing works
Upvotes: 1
Views: 176
Reputation: 1044
You need to unquote the dynamic key argument, either with the UQ function or with the equivalent double-bang notation:
library(dplyr)
library(tidyr)
dynamicKey = "coolKey"
mtcars[,8:11] %>% gather(key = !!dynamicKey,value = values,matches("vs|am"))
Output:
gear carb coolKey values
1 4 4 vs 0
2 4 4 vs 0
3 4 1 vs 1
4 3 1 vs 1
5 3 2 vs 0
6 3 1 vs 1 ...
Same thing but with UQ instead of the double bang:
> mtcars[,8:11] %>% gather(key = UQ(dynamicKey),value = values,matches("vs|am"))
gear carb coolKey values
1 4 4 vs 0
2 4 4 vs 0
3 4 1 vs 1
4 3 1 vs 1
5 3 2 vs 0
6 3 1 vs 1...
Upvotes: 2