我有这样结构的长格式数据(当然,多个国家、年份和变量):
df <- tribble(
~ind, ~country, ~year, ~group, ~dummy, ~v1, ~v2,
1, "country 1", 1990, "A", 0, 2.53, 1.68,
2, "country 1", 1990, "B", 0, 2.63, -5.21,
3, "country 1", 1991, "A", 1, 6.54, 3.48,
4, "country 1", 1991, "B", 1, 7.11, 2.52,
5, "country 1", 1992, "A", 0, 2.69, -3.45,
6, "country 1", 1992, "A", 0, 2.49, -3.45,
7, "country 2", 1990, "A", 0, 2.73, 1.68,
8, "country 2", 1990, "B", 0, 2.73, -1.21,
9, "country 2", 1991, "A", 1, 6.44, 3.28,
10, "country 2", 1991, "B", 1, 3.11, 2.51,
11, "country 2", 1992, "A", 0, 2.64, -3.45,
12, "country 2", 1992, "A", 0, 2.39, 2.85
)
我想将其转换为更宽的格式,结构如下:
ind country year A_dummy B_dummy A_v1 A_v2 B_v1 B_v2
1 country 1 1990 0 0 2.53 1.68 2.63 -5.21
2 country 2 1990 0 0 2.73 1.68 2.73 -1.21
3 country 1 1991 1 1 6.54 3.48 7.11 2.52
4 country 2 1991 1 1 6.44 3.28 3.11 2.51
# etc.
其中每一行代表一个国家-年,列代表不同的变量,每个组都有一个后缀。我想我必须使用 pivot_wider()
,但不知道如何保留国家/地区-年份组合。
有人能指出我正确的方向吗?
答案 0 :(得分:2)
您可以在 values_from
中的 pivot_wider
中传递多个值。
tidyr::pivot_wider(df, names_from = group, values_from = c(dummy, v1, v2))
答案 1 :(得分:2)
df %>%
pivot_wider(names_from = group,
values_from = c(dummy, v1, v2))