使用Reshape Cast

时间:2017-01-25 19:54:33

标签: r reshape reshape2

我正在尝试通过以下方式重构示例数据框。

df<-data.frame(market = c("a","b","c","a","b","c"),companyName =  c("foo","foo","foo", "bar","bar","bar"), val = seq(1,6))
require(reshape)
dfNew <- cast(df,market ~ companyName+companyName)

生成:

      market        company 1   company 2    
1      a                1           4
2      b                2           5
3      c                3           6

但是我收到了这个错误:

Using val as value column.  Use the value argument to cast to override this choice
Error in `[.data.frame`(data, , variables, drop = FALSE) : 
  undefined columns selected

1 个答案:

答案 0 :(得分:0)

此时

reshapereshape2都是已弃用的软件包。如果您使用Hadley的最新版本tidyr

spread(df, key = companyName, value = val)

  market bar foo
1      a   4   1
2      b   5   2
3      c   6   3