转换错误 - .subset2(x, i, 精确 = 精确) 中的错误

Ale*_*x W 5 r reshape2

感觉我在这里遗漏了一些明显的东西,所以提前道歉。无论如何,这是a我尝试投射的一些数据:

acct_num     year_prem    prem       exc
001          2012         2763585 exclusive
001          2011         2377688 exclusive
001          2010         2083065 exclusive
001          2009         1751722 exclusive
001          2008         1639484 exclusive
Run Code Online (Sandbox Code Playgroud)

然而,转换给了我一个我无法弄清楚/解释的错误:

b <- dcast(a, formula= acct_num + exc ~ year_prem, value.var= prem, fill= NA)

Error in .subset2(x, i, exact = exact) : invalid subscript type 'list'
Run Code Online (Sandbox Code Playgroud)

我认为我不需要fill= NA。但不管有没有它我都会遇到同样的错误。任何帮助,将不胜感激。

Did*_*rts 5

你应该加上prem引号然后它就起作用了。函数dcast()期望这value.var=将是列的名称(如此引用)。

> dcast(a, formula= acct_num + exc ~ year_prem, value.var= "prem", fill= NA)
  acct_num       exc    2008    2009    2010    2011    2012
1        1 exclusive 1639484 1751722 2083065 2377688 2763585
Run Code Online (Sandbox Code Playgroud)