【问题标题】:Using Reshape Cast [duplicate]使用 Reshape Cast [重复]
【发布时间】:2017-01-25 19:54:33
【问题描述】:

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

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

【问题讨论】:

    标签: r reshape reshape2


    【解决方案1】:

    reshapereshape2 目前都是弃用的软件包。如果你使用 Hadley 的最新版本,tidyr

    spread(df, key = companyName, value = val)
    
      market bar foo
    1      a   4   1
    2      b   5   2
    3      c   6   3
    

    【讨论】:

      猜你喜欢
      • 2012-06-19
      • 1970-01-01
      • 2013-12-07
      • 2014-03-01
      • 2023-04-09
      • 2019-10-17
      • 1970-01-01
      • 2017-11-22
      • 2012-02-09
      相关资源
      最近更新 更多