我想以编程方式使用dplyr::select
重新排列df的列
非R用户将执行代码,此人将提供以下两个输入:
report.month <- "Jul"
report.year <- 2017
只有月份会改变,df中的所有其他名称都是相同的
df1 <- data.frame(
country = "AU",
Jul_2017 = 500,
Customer = "some guy")
country Customer Jul_2017
AU some guy 500
reporting.month.name <- as.symbol(paste(report.month, report.year, sep = "_"))
df1 %>% select(country, Customer, reporting.month.name)
Error: `reporting.month.name` must resolve to integer column positions, not a symbol
非常感谢任何建议/帮助
答案 0 :(得分:2)
从代码中删除as.symbol(),如下所示:
reporting.month.name <- paste(report.month, report.year, sep = "_")
df1 %>% select(country, Customer, reporting.month.name)
输出:
country Customer Jul_2017
1 AU some guy 500