问题
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