Using Reshape Cast [duplicate]

2019-09-14 16:30发布

This question already has an answer here:

I am trying to reshape an example dataframe by the following.

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)

To generate:

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

But I get this error:

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条回答
霸刀☆藐视天下
2楼-- · 2019-09-14 16:53

reshape and reshape2 are both deprecated packages at this point. If you use Hadley's latest version, tidyr:

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

  market bar foo
1      a   4   1
2      b   5   2
3      c   6   3
查看更多
登录 后发表回答