1

我的原始数据框是这样的:x

Team  Date   variable  value
A 2012-07-01 Score      13
A 2012-07-02 Score      12
A 2012-07-03 Score      2097
A 2012-07-04 Score      45
A 2012-07-05 Score      41
A 2012-07-06 Score      763

需要这样

z
    Team 2012-07-01 2012-07-02 2012-07-03 2012-07-04 2012-07-05 2012-07-06 
    A     13         12          2097      45        41         763

library(reshape)
z<-cast(x, Team + variable ~ Date)

我收到一条警告消息,说明:

聚合需要 fun.aggregate:默认使用的长度

4

2 回答 2

1

使用dcastreshape2 包,它会工作!

x <- read.table(text='Team  Date   variable  value
A 2012-07-01 Score      13
A 2012-07-02 Score      12
A 2012-07-03 Score      2097
A 2012-07-04 Score      45
A 2012-07-05 Score      41
A 2012-07-06 Score      763', header=T)

library(reshape2)
dcast(x, Team + variable ~ Date)
 Team variable 2012-07-01 2012-07-02 2012-07-03 2012-07-04 2012-07-05 2012-07-06
1    A    Score         13         12       2097         45         41        763

编辑:

您可以使用reshape统计数据中的函数。如上所述,不需要额外的包。

Y <- reshape(x, v.names='value', idvar='Team', timevar='Date',direction='wide')
names(Y) <- sub('value.', '', names(Y))
Y
  Team variable 2012-07-01 2012-07-02 2012-07-03 2012-07-04 2012-07-05 2012-07-06
1    A    Score         13         12       2097         45         41        763
于 2012-10-12T15:55:20.430 回答
0

无需特殊包装:

xtabs(value ~ Team + Date, x)
#     Date
# Team 2012-07-01 2012-07-02 2012-07-03 2012-07-04 2012-07-05 2012-07-06
#    A         13         12       2097         45         41        763
as.data.frame.matrix(xtabs(value ~ Team + Date, x))
#   2012-07-01 2012-07-02 2012-07-03 2012-07-04 2012-07-05 2012-07-06
# A         13         12       2097         45         41        763
于 2012-10-12T16:04:56.927 回答