简体   繁体   中英

Factor to columns and columns to factor

I have this dataframe

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

I want to convert the factor Type to columns and the columns N1 to N3 to a factor. The desired final result should look like this:

  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

I've been trying to accomplished that by combining plyr, reshape, dcast, melt and so on but I could find the right way. Thank you very much

Here's the "reshape2" approach:

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

Here is a base R approach using reshape() and aggregate() . The row order can be fixed later using 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM