这是此示例的玩具数据集:
data <- data.frame(x=rep(c("red","blue","green"),each=4), y=rep(letters[1:4],3), value.1 = 1:12, value.2 = 13:24)
x y value.1 value.2
1 red a 1 13
2 red b 2 14
3 red c 3 15
4 red d 4 16
5 blue a 5 17
6 blue b 6 18
7 blue c 7 19
8 blue d 8 20
9 green a 9 21
10 green b 10 22
11 green c 11 23
12 green d 12 24
如何转换或传播变量 y,以生成以下宽 data.frame:
x a.value.1 b.value.1 c.value.1 d.value.1 a.value.2 b.value.2 c.value.2 d.value.2
1 blue 5 6 7 8 17 18 19 20
2 green 9 10 11 12 21 22 23 24
3 red 1 2 3 4 13 14 15 16