通常使用dplyr/tidyr,我可以通过否定来实现排除
... %>% gather(x, -y)
Run Code Online (Sandbox Code Playgroud)
但是,目前,我希望以编程方式指定一些变量,并且理想情况下是排除
...%>%gather_(xVar,-yVar)
其中xVar和yVar是字符变量(例如,值为'x'和'y').
是否仅仅使用字符串版本的函数禁止排除,或者有没有办法执行它们?
无论是明显的罪魁祸首-yVar,并paste0('-', yVar)似乎产生错误.
我最近也遇到了同样的问题。我使用了自己计算包含列的解决方法。这并不完全令人满意,但我认为目前这不可能实现gather_。问题似乎出在select_vars_函数上。您可以使用exclude中的选项来规避它select_vars_。
# creating sample data from example in gather
stocks <- data.frame(
time = as.Date('2009-01-01') + 0:9,
X = rnorm(10, 0, 1),
Y = rnorm(10, 0, 2),
Z = rnorm(10, 0, 4)
)
# original call using gather
gather(stocks, stock, price, -time)
# calculating select_vars yourself
stocks %>% gather_("stock",
"price",
names(.)[!"time" == names(.)])
# using exclude in select_vars_
stocks %>% gather_("stock",
"price",
select_vars_(names(.),
names(.),
exclude = "time"))
Run Code Online (Sandbox Code Playgroud)