Using Reshape Cast [duplicate]

痞子三分冷 提交于 2019-12-25 08:16:09

问题


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:


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


来源:https://stackoverflow.com/questions/41860480/using-reshape-cast

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!