CAST ERROR IN R [.data.frame`中的错误(数据,变量,丢弃= FALSE):选择了未定义的列

时间:2016-10-11 18:43:00

标签: r casting

使用包“Reshape”投射数据时,我在R中收到此错误:

"Error in `[.data.frame`(data, , variables, drop = FALSE) : 
  undefined columns selected"

Waterquality Data看起来像这样:

   year turbid.ave.mo month2
16 2002     926.66667   JUNE
23 2002     493.66667   JULY
29 2002     107.10000    AUG
37 2002     295.80000   SEPT
46 2002     528.28000    OCT
53 2002     266.20000    NOV
56 2002      73.80000    DEC
1  2003     115.00000    JAN

我想进行转换,以便月份是列标题(每年一行,JAN到DEC作为列标题。

我试过的代码:

casted = cast(Waterquality [ ,c(1:3)], year + turbid.ave.mo ~ month2, mean, fill =0, value = "turbid.ave.mo")

错误:[.data.frame中的错误(数据,变量,drop = FALSE):   选择了未定义的列

有人告诉我我做错了什么吗?在此先感谢!!

3 个答案:

答案 0 :(得分:1)

不要在公式中包含值:

   Still in the first while loop.
   Still in the first while loop.
   First while loop completed.
   Successfully executed.

答案 1 :(得分:1)

这是一个更清晰的tidyr解决方案:

library(tidyr)

spread(Waterquality, month2, turbid.ave.mo, fill = 0)
# year        Apr      Aug       Feb        Jan         Jul      Jun        Mar       May
# 1 2002 -0.2798722 0.000000 -0.901421 -0.9576323 -0.07627388 0.652116 -0.3758433 0.9795479
# 2 2003  0.0000000 1.047832  0.000000  0.0000000  0.00000000 0.000000  0.0000000 0.0000000

答案 2 :(得分:0)

有同样的问题。最初的数据为:(Id,Value,Variable) 只需将订单重新排列为(Id,Variable,Value)。

    cast (tmp, id ~ delivery_month)