如何在保持2根色谱柱的同时使用R中的熔融功能?

时间:2017-02-14 19:17:41

标签: r reshape2 melt

我的名字格式为:

  V1      V2     V3     V4      V5       V6      V7
NICK   PIPE   10 IN  PAINT   GREEN    SCREW   0.5 IN
MIKE   PAINT   RED   TOOL    HAMMER    PIPE    3 IN

我想使用融合功能,以便数据显示如下:

V1      V2     V3
NICK   PIPE   10 IN
NICK   PAINT  GREEN
NICK   SCREW   0.5 IN
MIKE   PAINT   RED
MIKE   TOOL    HAMMER
MIKE   PIPE    3 IN

1 个答案:

答案 0 :(得分:0)

我们可以使用base R方法将其重塑为“长”字样。格式

data.frame(V1 = rep(df1$V1, each = 3),
           V2= c(t(df1[-1][c(TRUE, FALSE)])), 
           V3 = c(t(df1[-1][c(FALSE, TRUE)])))
#    V1    V2     V3
#1 NICK  PIPE  10 IN
#2 NICK PAINT  GREEN
#3 NICK SCREW 0.5 IN
#4 MIKE PAINT    RED
#5 MIKE  TOOL HAMMER
#6 MIKE  PIPE   3 IN