通过在 R 中堆叠相似变量来重组数据集
Restructuring data set by stacking similar variables in R
我有以下变量作为更大数据集的一部分。每 3 个连续变量测量相同(例如前 3 个变量 c_0064、c_0065、c_0066 测量参与者知道的 3 个品牌,后 3 个变量 v_159_1、v_159_2, v_159_3 测量了参与者对前面提到的每个品牌的态度,等等。我只显示了数据集中的第一列和最后一列。在列 v_159_3 之后它继续实际上使用 v_160_1、v_160_2、v_160_3、v_161_1... 直到到达 v_182_1、v_182_2、v_182_3 列.
structure(list(lfdn = c(4, 6, 7, 8, 9, 11, 12, 19), c_0064 = c("x",
"t", "x", "x", "t", "x", "z", "z"), c_0065 = c("z", "z", "z",
"f", "f", "f", "t", "t"), c_0066 = c("x", "x", "x", "a", "f",
"t", "z", "b"), v_159_1 = c(1, 1, 3, 2, 2, 5, 4, 3), v_159_2 = c(3,
3, 3, 3, 3, 2, 5, 1), v_159_3 = c(5, 5, 1, 4, 4, 1, 2, 2), v_182_1 = c(1,
1, 5, 5, 4, 4, 4, 4), v_182_2 = c(4, 2, 2, 2, 2, 3, 1, 5), v_182_3 = c(5,
4, 5, 1, 2, 5, 2, 2)), row.names = c(NA, -8L), class = c("tbl_df",
"tbl", "data.frame"))
> df
# A tibble: 8 x 10
lfdn c_0064 c_0065 c_0066 v_159_1 v_159_2 v_159_3 v_182_1 v_182_2 v_182_3
<dbl> <chr> <chr> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 4 x z x 1 3 5 1 4 5
2 6 t z x 1 3 5 1 2 4
3 7 x z x 3 3 1 5 2 5
4 8 x f a 2 3 4 5 2 1
5 9 t f f 2 3 4 4 2 2
6 11 x f t 5 2 1 4 3 5
7 12 z t z 4 5 2 4 1 2
8 19 z t b 3 1 2 4 5 2
目标是 restructure/stack 始终显示 3 个类似的列,如下所示:
structure(list(lfdn = c(4, 6, 7, 8, 9, 11, 12, 19, 4, 6, 7, 8,
9, 11, 12, 19, 4, 6, 7, 8, 9, 11, 12, 19), c_0064_65_66 = c("x",
"t", "x", "x", "t", "x", "z", "z", "z", "z", "z", "f", "f", "f",
"t", "t", "x", "x", "x", "a", "f", "t", "z", "b"), v_159_1_2_3 = c(1,
1, 3, 2, 2, 5, 4, 3, 3, 3, 3, 3, 3, 2, 5, 1, 5, 5, 1, 4, 4, 1,
2, 2), v_181_1_2_3 = c(1, 1, 5, 5, 4, 4, 4, 4, 4, 2, 2, 2, 2,
3, 1, 5, 5, 4, 5, 1, 2, 5, 2, 2)), row.names = c(NA, -24L), class = c("tbl_df",
"tbl", "data.frame"))
> dflong
# A tibble: 24 x 4
lfdn c_0064_65_66 v_159_1_2_3 v_181_1_2_3
<dbl> <chr> <dbl> <dbl>
1 4 x 1 1
2 6 t 1 1
3 7 x 3 5
4 8 x 2 5
5 9 t 2 4
6 11 x 5 4
7 12 z 4 4
8 19 z 3 4
9 4 z 3 4
10 6 z 3 2
# ... with 14 more rows
我已经无法融化数据,所以我想到的唯一方法是使用 stack 命令并堆叠每个以下 3 个变量,如 stack(df, select=c("c_0064", "c_0065", "c_0066"))
,然后将这些堆叠的变量放在最后。但我希望有一种更经济的方法来做到这一点,因为除了显示的变量之外,我在数据集中还有更多的“重复”变量。
您可以将 pivot_longer
与 names_pattern
一起使用。根据数据中的列名,使用模式准确捕获列名。
tidyr::pivot_longer(df, cols = -lfdn,
names_to = '.value', names_pattern = '(c|[a-z]_\d+)')
# lfdn c v_159 v_182
# <dbl> <chr> <dbl> <dbl>
# 1 4 x 1 1
# 2 4 z 3 4
# 3 4 x 5 5
# 4 6 t 1 1
# 5 6 z 3 2
# 6 6 x 5 4
# 7 7 x 3 5
# 8 7 z 3 2
# 9 7 x 1 5
#10 8 x 2 5
# … with 14 more rows
我有以下变量作为更大数据集的一部分。每 3 个连续变量测量相同(例如前 3 个变量 c_0064、c_0065、c_0066 测量参与者知道的 3 个品牌,后 3 个变量 v_159_1、v_159_2, v_159_3 测量了参与者对前面提到的每个品牌的态度,等等。我只显示了数据集中的第一列和最后一列。在列 v_159_3 之后它继续实际上使用 v_160_1、v_160_2、v_160_3、v_161_1... 直到到达 v_182_1、v_182_2、v_182_3 列.
structure(list(lfdn = c(4, 6, 7, 8, 9, 11, 12, 19), c_0064 = c("x",
"t", "x", "x", "t", "x", "z", "z"), c_0065 = c("z", "z", "z",
"f", "f", "f", "t", "t"), c_0066 = c("x", "x", "x", "a", "f",
"t", "z", "b"), v_159_1 = c(1, 1, 3, 2, 2, 5, 4, 3), v_159_2 = c(3,
3, 3, 3, 3, 2, 5, 1), v_159_3 = c(5, 5, 1, 4, 4, 1, 2, 2), v_182_1 = c(1,
1, 5, 5, 4, 4, 4, 4), v_182_2 = c(4, 2, 2, 2, 2, 3, 1, 5), v_182_3 = c(5,
4, 5, 1, 2, 5, 2, 2)), row.names = c(NA, -8L), class = c("tbl_df",
"tbl", "data.frame"))
> df
# A tibble: 8 x 10
lfdn c_0064 c_0065 c_0066 v_159_1 v_159_2 v_159_3 v_182_1 v_182_2 v_182_3
<dbl> <chr> <chr> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 4 x z x 1 3 5 1 4 5
2 6 t z x 1 3 5 1 2 4
3 7 x z x 3 3 1 5 2 5
4 8 x f a 2 3 4 5 2 1
5 9 t f f 2 3 4 4 2 2
6 11 x f t 5 2 1 4 3 5
7 12 z t z 4 5 2 4 1 2
8 19 z t b 3 1 2 4 5 2
目标是 restructure/stack 始终显示 3 个类似的列,如下所示:
structure(list(lfdn = c(4, 6, 7, 8, 9, 11, 12, 19, 4, 6, 7, 8,
9, 11, 12, 19, 4, 6, 7, 8, 9, 11, 12, 19), c_0064_65_66 = c("x",
"t", "x", "x", "t", "x", "z", "z", "z", "z", "z", "f", "f", "f",
"t", "t", "x", "x", "x", "a", "f", "t", "z", "b"), v_159_1_2_3 = c(1,
1, 3, 2, 2, 5, 4, 3, 3, 3, 3, 3, 3, 2, 5, 1, 5, 5, 1, 4, 4, 1,
2, 2), v_181_1_2_3 = c(1, 1, 5, 5, 4, 4, 4, 4, 4, 2, 2, 2, 2,
3, 1, 5, 5, 4, 5, 1, 2, 5, 2, 2)), row.names = c(NA, -24L), class = c("tbl_df",
"tbl", "data.frame"))
> dflong
# A tibble: 24 x 4
lfdn c_0064_65_66 v_159_1_2_3 v_181_1_2_3
<dbl> <chr> <dbl> <dbl>
1 4 x 1 1
2 6 t 1 1
3 7 x 3 5
4 8 x 2 5
5 9 t 2 4
6 11 x 5 4
7 12 z 4 4
8 19 z 3 4
9 4 z 3 4
10 6 z 3 2
# ... with 14 more rows
我已经无法融化数据,所以我想到的唯一方法是使用 stack 命令并堆叠每个以下 3 个变量,如 stack(df, select=c("c_0064", "c_0065", "c_0066"))
,然后将这些堆叠的变量放在最后。但我希望有一种更经济的方法来做到这一点,因为除了显示的变量之外,我在数据集中还有更多的“重复”变量。
您可以将 pivot_longer
与 names_pattern
一起使用。根据数据中的列名,使用模式准确捕获列名。
tidyr::pivot_longer(df, cols = -lfdn,
names_to = '.value', names_pattern = '(c|[a-z]_\d+)')
# lfdn c v_159 v_182
# <dbl> <chr> <dbl> <dbl>
# 1 4 x 1 1
# 2 4 z 3 4
# 3 4 x 5 5
# 4 6 t 1 1
# 5 6 z 3 2
# 6 6 x 5 4
# 7 7 x 3 5
# 8 7 z 3 2
# 9 7 x 1 5
#10 8 x 2 5
# … with 14 more rows