【问题标题】:Factor to columns and columns to factor因子到列和列到因子
【发布时间】:2012-12-18 09:10:59
【问题描述】:

我有这个数据框

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 等来实现这一点,但我可以找到正确的方法。非常感谢

【问题讨论】:

  • 每当您使用像sample 这样的随机数生成功能时,请考虑在示例数据中使用set.seed()

标签: r dataframe reshape


【解决方案1】:

这是“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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-04-12
    • 2013-08-03
    • 1970-01-01
    • 2022-01-07
    • 1970-01-01
    • 2021-04-03
    • 2019-02-18
    • 1970-01-01
    相关资源
    最近更新 更多