我试图在两列(var 1和question)上使用count,其中一列(问题)是动态的。当变量名称不包含空格时,下面的代码工作,但由于它们包含空格,因此存在错误。
df <- data.frame(`var 1` = c('a','b'),
`var 2` = c('a','b'),
`var 3` = c('a','b'),
`var 4` = c('a','b')
)
question <- 'var 3'
dfDynamic <- df %>% count_(vars = c('var 1', question))
我得到的错误如下:
Error in parse(text = x) : <text>:1:5: unexpected numeric constant
1: var 1
有什么方法可以解决这个问题吗?
答案 0 :(得分:2)
您可以使用&#34; quasiquotation&#34;来解决它,这是处理动态列命名的官方方法。
示例:
df <- data.frame(`var 1` = c('a','b'),
`var 2` = c('a','b'),
`var 3` = c('a','b'),
`var 4` = c('a','b')
)
# Does not work:
question <- 'var 3'
dfDynamic <- df %>% count_(vars = c('var 1', question))
# Try this instead:
# "quote" the column name -In dplyr lingo, create a "quosure"
question <- quo('var 3')
# Notice that you don't need to use "count_" anymore,
# just invoke the quoted column name with "!!"
dfDynamic <- df %>% count('var 1', !!question)
dfDynamic
你会得到:
# A tibble: 1 x 3
`"var 1"` `"var 3"` n
<chr> <chr> <int>
1 var 1 var 3 2
这是一个包含更多示例的教程,也解释了quasiquotation背后的逻辑:http://dplyr.tidyverse.org/articles/programming.html