1

我想解开一个 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 的列表?

4

2 回答 2

5

你想要包裹reshape2

require(reshape2)
melt(HairEyeColor)

注释中的澄清确认您想要的是获取行并重复它们,而不是将该信息压缩value列中(默认情况下)。

为此,正如 Tyler 在评论中所说,请执行以下操作:

x <- melt(HairEyeColor)
x[rep(seq_len(nrow(x)), x$value), -4]

第一行 make x,打印在下面。

第二行告诉R重复第一行(Hair, Eye,的组合Sexvalue次。-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
于 2013-11-06T19:00:48.113 回答
5

简单的基础 R 解决方案

as.data.frame(HairEyeColor)
于 2013-11-06T19:19:52.660 回答