要考虑列和列的因素

时间:2012-12-18 09:11:00

标签: r dataframe reshape

我有这个数据框

DFtest <- data.frame(Zone=rep(c("R1","R2","R3"),each=2),
                     Type=rep(c("C1","C2"),3),
                     N1=sample(1:6),
                     N2=sample(seq(0,1,length.out=6)),
                     N3=sample(letters[1:6]))
DFtest

  Zone Type N1  N2 N3
1   R1   C1  2 0.4  c
2   R1   C2  5 1.0  a
3   R2   C1  4 0.6  e
4   R2   C2  3 0.2  d
5   R3   C1  1 0.0  b
6   R3   C2  6 0.8  f

我想将因子类型转换为列,将列N1到N3转换为因子。期望的最终结果应如下所示:

  Zone   Ns Type.C1 Type.C2
1   R1   N1       2       5
2   R1   N2     0.4     1.0
3   R1   N3       c       a
4   R2   N1       4       3
5   R2   N2     0.6     0.2
6   R2   N3       e       d
7   R3   N1       1       6
8   R3   N2     0.0     0.8
9   R3   N3       b       f

我一直试图通过结合plyr,reshape,dcast,melt等来实现这一目标,但我能找到正确的方法。非常感谢你

1 个答案:

答案 0 :(得分:8)

这是“reshape2”方法:

library(reshape2)
DFtestL <- melt(DFtest, id.vars=1:2)
dcast(DFtestL, Zone + variable ~ Type)
#   Zone variable  C1  C2
# 1   R1       N1   2   3
# 2   R1       N2 0.2 0.4
# 3   R1       N3   c   d
# 4   R2       N1   1   6
# 5   R2       N2   0 0.8
# 6   R2       N3   a   b
# 7   R3       N1   5   4
# 8   R3       N2 0.6   1
# 9   R3       N3   f   e

以下是使用reshape()aggregate()的基本R方法。可以稍后使用order()修复行顺序。

DFtestL2 <- reshape(DFtest, direction = "long", 
                    idvar=c("Zone", "Type"), 
                    varying=3:ncol(DFtest), sep="")
aggregate(N ~ Zone + time, DFtestL2, I)
#   Zone time N.1 N.2
# 1   R1    1   2   3
# 2   R2    1   1   6
# 3   R3    1   5   4
# 4   R1    2 0.2 0.4
# 5   R2    2   0 0.8
# 6   R3    2 0.6   1
# 7   R1    3   c   d
# 8   R2    3   a   b
# 9   R3    3   f   e