我想解开R数据集并以长格式获取它。例如,如果我输入HairEyeColor,我会得到
, , Sex = Male
Eye
Hair Brown Blue Hazel Green
Black 32 11 10 3
Brown 53 50 25 15
Red 10 10 7 7
Blond 3 30 5 8
, , Sex = Female
Eye
Hair Brown Blue Hazel Green
Black 36 9 5 2
Brown 66 34 29 14
Red 16 7 7 7
Blond 4 64 5 8
如何获得长格式的所有542的列表?
答案 0 :(得分:5)
您需要包reshape2
require(reshape2)
melt(HairEyeColor)
评论中的澄清确认您想要的是获取行并重复它们,而不是在value
列中使用压缩信息(默认情况下)。
要做到这一点,请在评论中作为Tyler执行以下操作:
x <- melt(HairEyeColor)
x[rep(seq_len(nrow(x)), x$value), -4]
第一行显示x
,打印在下方。
第二行告诉R
重复第一行(Hair
,Eye
,Sex
)value
次的组合。逗号后的-4
(如[..., -4]
)中的含义是 NOT 在重复行时包含第四列(在本例中为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
答案 1 :(得分:5)
简单的基础R解决方案
as.data.frame(HairEyeColor)