你想要包裹reshape2
require(reshape2)
melt(HairEyeColor)
注释中的澄清确认您想要的是获取行并重复它们,而不是将该信息压缩在value
列中(默认情况下)。
为此,正如 Tyler 在评论中所说,请执行以下操作:
x <- melt(HairEyeColor)
x[rep(seq_len(nrow(x)), x$value), -4]
第一行 make x
,打印在下面。
第二行告诉R
重复第一行(Hair
, Eye
,的组合Sex
)value
次。-4
逗号后(如[..., -4]
)的含义是在重复行时不包括第四列(在这种情况下)。value
这是x
默认值的输出melt
。
> melt(HairEyeColor)
Hair Eye Sex value
1 Black Brown Male 32
2 Brown Brown Male 53
3 Red Brown Male 10
4 Blond Brown Male 3
5 Black Blue Male 11
6 Brown Blue Male 50
7 Red Blue Male 10
8 Blond Blue Male 30
9 Black Hazel Male 10
10 Brown Hazel Male 25
11 Red Hazel Male 7
12 Blond Hazel Male 5
13 Black Green Male 3
14 Brown Green Male 15
15 Red Green Male 7
16 Blond Green Male 8
17 Black Brown Female 36
18 Brown Brown Female 66
19 Red Brown Female 16
20 Blond Brown Female 4
21 Black Blue Female 9
22 Brown Blue Female 34
23 Red Blue Female 7
24 Blond Blue Female 64
25 Black Hazel Female 5
26 Brown Hazel Female 29
27 Red Hazel Female 7
28 Blond Hazel Female 5
29 Black Green Female 2
30 Brown Green Female 14
31 Red Green Female 7
32 Blond Green Female 8