R 如何将具有多个列的更宽 table 重塑为具有多个变量的更长 table
R How to reshape wider table with multiple columns into longer table with multiple variables
我的数据库在相同的监测地块上包含 2 个测量周期。在每个监测地块上,我们测量枯木的体积和面积以及在枯木上再生的树种再生数量。枯木分3级分解。我需要将更宽的 table 转换为更长的 table。
在这里阅读了很多帖子后,我几乎可以肯定解决方案在 tidyr 包和 pivot_longer 函数中。我将非常感谢你的帮助。
picture of dw_wider table and dw_longer table
这里是更宽的代码 table:
dw_wider <- structure(list(Period = c(
1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L,
2L, 2L, 2L),
DwType = c(
"1_trunks", "1_trunks", "1_trunks", "2_stumps", "2_stumps", "2_stumps", "1_trunks", "1_trunks", "1_trunks", "2_stumps", "2_stumps", "2_stumps"),
Plot = c(
101L, 102L, 103L, 101L, 102L, 103L, 101L, 102L, 103L, 101L, 102L, 103L),
m3ha_dc1 = c(
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L),
m3ha_dc2 = c(
185.9886004,
36.14392074, 13.09592051, 61.99620013, 12.04797358, 4.365306836,
399.9427251, 187.306535, 59.38662, 133.3142417, 62.43551166,
19.79554),
m3ha_dc3 = c(
0.160849544, 16.04894292, 10.76337767,
0.053616515, 5.349647639, 3.587792556, 75.0880521, 27.88578286,
32.00025432, 25.0293507, 9.295260953, 10.66675144),
m2ha_dc1 = c(0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), m2ha_dc2 = c(669.9940464,
255.4172853, 147.8409739, 167.4985116, 63.85432132, 36.96024346,
1697.643681, 734.7850821, 303.60205, 424.4109202, 183.6962705,
75.9005125),
m2ha_dc3 = c(
0.40212386, 74.99540121, 87.88647158,
0.100530965, 18.7488503, 21.97161789, 297.8308267, 148.4095964,
132.5277239, 74.45770669, 37.10239909, 33.13193096),
pcsm2_dc1 = c(
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L),
pcsm2_dc2 = c(
0.895530346, 0, 0, 0, 0.326626119, 0.065875708, 1.432848553, 0, 0, 0, 0.544376866, 0.263502832),
pcsm2_dc3 = c(
0, 0, 12.06101441, 0, 1.212859575, 0.30182364, 0, 0, 18.20530477, 0, 2.1561948, 0.60364728)),
class = "data.frame", row.names = c(NA,
-12L))
这是我想要达到的结果:
dw_longer <- structure(list(Period = c(
1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L,
2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 1L,
1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L),
DwType = c(
"1_trunks", "1_trunks", "1_trunks", "2_stumps", "2_stumps", "2_stumps", "1_trunks", "1_trunks", "1_trunks", "2_stumps", "2_stumps", "2_stumps", "1_trunks", "1_trunks", "1_trunks", "2_stumps", "2_stumps", "2_stumps", "1_trunks", "1_trunks", "1_trunks", "2_stumps", "2_stumps", "2_stumps", "1_trunks", "1_trunks", "1_trunks", "2_stumps", "2_stumps", "2_stumps", "1_trunks", "1_trunks", "1_trunks", "2_stumps", "2_stumps", "2_stumps"),
Plot = c(
101L, 102L, 103L, 101L, 102L, 103L, 101L, 102L, 103L, 101L, 102L, 103L, 101L, 102L, 103L, 101L, 102L, 103L, 03L, 101L, 102L, 103L, 101L, 102L, 103L, 101L, 102L, 103L, 101L, 102L, 103L, 101L, 102L, 103L),
dc = c(
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L),
m3ha = c(
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 185.9886004, 36.14392074, 13.09592051, 61.99620013, 12.04797358, 4.365306836, 399.9427251, 187.306535, 59.38662, 133.3142417, 62.43551166, 19.79554, 0.160849544, 16.04894292, 10.76337767, 0.053616515, 5.349647639, 3.587792556, 75.0880521, 27.88578286, 32.00025432, 25.0293507, 9.295260953, 10.66675144),
m2ha = c(
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 669.9940464, 255.4172853, 147.8409739, 167.4985116, 63.85432132, 36.96024346, 1697.643681, 734.7850821, 303.60205, 424.4109202, 183.6962705, 75.9005125, 0.40212386, 74.99540121, 87.88647158, 0.100530965, 18.7488503, 21.97161789, 297.8308267, 148.4095964, 132.5277239,
74.45770669, 37.10239909, 33.13193096),
pcsha = c(
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.895530346, 0, 0, 0, 0.326626119,
0.065875708, 1.432848553, 0, 0, 0, 0.544376866, 0.263502832, 0, 0, 12.06101441, 0, 1.212859575, 0.30182364, 0, 0, 18.20530477, 0, 2.1561948, 0.60364728)),
class = "data.frame", row.names = c(NA, -36L))
试试这个:
library(tidyverse)
dw_wider <- structure(list(Period = c(
1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L,
2L, 2L, 2L
), DwType = c(
"1_trunks", "1_trunks", "1_trunks", "2_stumps",
"2_stumps", "2_stumps", "1_trunks", "1_trunks", "1_trunks", "2_stumps",
"2_stumps", "2_stumps"
), Plot = c(
101L, 102L, 103L, 101L, 102L,
103L, 101L, 102L, 103L, 101L, 102L, 103L
), m3ha_dc1 = c(
0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L
), m3ha_dc2 = c(
185.9886004,
36.14392074, 13.09592051, 61.99620013, 12.04797358, 4.365306836,
399.9427251, 187.306535, 59.38662, 133.3142417, 62.43551166,
19.79554
), m3ha_dc3 = c(
0.160849544, 16.04894292, 10.76337767,
0.053616515, 5.349647639, 3.587792556, 75.0880521, 27.88578286,
32.00025432, 25.0293507, 9.295260953, 10.66675144
), m2ha_dc1 = c(
0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L
), m2ha_dc2 = c(
669.9940464,
255.4172853, 147.8409739, 167.4985116, 63.85432132, 36.96024346,
1697.643681, 734.7850821, 303.60205, 424.4109202, 183.6962705,
75.9005125
), m2ha_dc3 = c(
0.40212386, 74.99540121, 87.88647158,
0.100530965, 18.7488503, 21.97161789, 297.8308267, 148.4095964,
132.5277239, 74.45770669, 37.10239909, 33.13193096
), pcsm2_dc1 = c(
0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L
), pcsm2_dc2 = c(
0.895530346,
0, 0, 0, 0.326626119, 0.065875708, 1.432848553, 0, 0, 0, 0.544376866,
0.263502832
), pcsm2_dc3 = c(
0, 0, 12.06101441, 0, 1.212859575,
0.30182364, 0, 0, 18.20530477, 0, 2.1561948, 0.60364728
)), class = "data.frame", row.names = c(
NA,
-12L
))
dw_wider |>
pivot_longer(-c(Period, Plot, DwType), names_pattern = "(.*)_dc(.+)", names_to = c("col", "dc")) |>
pivot_wider(names_from = col, values_from = value) |>
arrange(dc, Period, Plot, DwType)
#> # A tibble: 36 × 7
#> Period DwType Plot dc m3ha m2ha pcsm2
#> <int> <chr> <int> <chr> <dbl> <dbl> <dbl>
#> 1 1 1_trunks 101 1 0 0 0
#> 2 1 2_stumps 101 1 0 0 0
#> 3 1 1_trunks 102 1 0 0 0
#> 4 1 2_stumps 102 1 0 0 0
#> 5 1 1_trunks 103 1 0 0 0
#> 6 1 2_stumps 103 1 0 0 0
#> 7 2 1_trunks 101 1 0 0 0
#> 8 2 2_stumps 101 1 0 0 0
#> 9 2 1_trunks 102 1 0 0 0
#> 10 2 2_stumps 102 1 0 0 0
#> # … with 26 more rows
由 reprex package (v2.0.1)
创建于 2022-05-07
我的数据库在相同的监测地块上包含 2 个测量周期。在每个监测地块上,我们测量枯木的体积和面积以及在枯木上再生的树种再生数量。枯木分3级分解。我需要将更宽的 table 转换为更长的 table。 在这里阅读了很多帖子后,我几乎可以肯定解决方案在 tidyr 包和 pivot_longer 函数中。我将非常感谢你的帮助。
picture of dw_wider table and dw_longer table
这里是更宽的代码 table:
dw_wider <- structure(list(Period = c(
1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L,
2L, 2L, 2L),
DwType = c(
"1_trunks", "1_trunks", "1_trunks", "2_stumps", "2_stumps", "2_stumps", "1_trunks", "1_trunks", "1_trunks", "2_stumps", "2_stumps", "2_stumps"),
Plot = c(
101L, 102L, 103L, 101L, 102L, 103L, 101L, 102L, 103L, 101L, 102L, 103L),
m3ha_dc1 = c(
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L),
m3ha_dc2 = c(
185.9886004,
36.14392074, 13.09592051, 61.99620013, 12.04797358, 4.365306836,
399.9427251, 187.306535, 59.38662, 133.3142417, 62.43551166,
19.79554),
m3ha_dc3 = c(
0.160849544, 16.04894292, 10.76337767,
0.053616515, 5.349647639, 3.587792556, 75.0880521, 27.88578286,
32.00025432, 25.0293507, 9.295260953, 10.66675144),
m2ha_dc1 = c(0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), m2ha_dc2 = c(669.9940464,
255.4172853, 147.8409739, 167.4985116, 63.85432132, 36.96024346,
1697.643681, 734.7850821, 303.60205, 424.4109202, 183.6962705,
75.9005125),
m2ha_dc3 = c(
0.40212386, 74.99540121, 87.88647158,
0.100530965, 18.7488503, 21.97161789, 297.8308267, 148.4095964,
132.5277239, 74.45770669, 37.10239909, 33.13193096),
pcsm2_dc1 = c(
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L),
pcsm2_dc2 = c(
0.895530346, 0, 0, 0, 0.326626119, 0.065875708, 1.432848553, 0, 0, 0, 0.544376866, 0.263502832),
pcsm2_dc3 = c(
0, 0, 12.06101441, 0, 1.212859575, 0.30182364, 0, 0, 18.20530477, 0, 2.1561948, 0.60364728)),
class = "data.frame", row.names = c(NA,
-12L))
这是我想要达到的结果:
dw_longer <- structure(list(Period = c(
1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L,
2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 1L,
1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L),
DwType = c(
"1_trunks", "1_trunks", "1_trunks", "2_stumps", "2_stumps", "2_stumps", "1_trunks", "1_trunks", "1_trunks", "2_stumps", "2_stumps", "2_stumps", "1_trunks", "1_trunks", "1_trunks", "2_stumps", "2_stumps", "2_stumps", "1_trunks", "1_trunks", "1_trunks", "2_stumps", "2_stumps", "2_stumps", "1_trunks", "1_trunks", "1_trunks", "2_stumps", "2_stumps", "2_stumps", "1_trunks", "1_trunks", "1_trunks", "2_stumps", "2_stumps", "2_stumps"),
Plot = c(
101L, 102L, 103L, 101L, 102L, 103L, 101L, 102L, 103L, 101L, 102L, 103L, 101L, 102L, 103L, 101L, 102L, 103L, 03L, 101L, 102L, 103L, 101L, 102L, 103L, 101L, 102L, 103L, 101L, 102L, 103L, 101L, 102L, 103L),
dc = c(
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L),
m3ha = c(
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 185.9886004, 36.14392074, 13.09592051, 61.99620013, 12.04797358, 4.365306836, 399.9427251, 187.306535, 59.38662, 133.3142417, 62.43551166, 19.79554, 0.160849544, 16.04894292, 10.76337767, 0.053616515, 5.349647639, 3.587792556, 75.0880521, 27.88578286, 32.00025432, 25.0293507, 9.295260953, 10.66675144),
m2ha = c(
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 669.9940464, 255.4172853, 147.8409739, 167.4985116, 63.85432132, 36.96024346, 1697.643681, 734.7850821, 303.60205, 424.4109202, 183.6962705, 75.9005125, 0.40212386, 74.99540121, 87.88647158, 0.100530965, 18.7488503, 21.97161789, 297.8308267, 148.4095964, 132.5277239,
74.45770669, 37.10239909, 33.13193096),
pcsha = c(
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.895530346, 0, 0, 0, 0.326626119,
0.065875708, 1.432848553, 0, 0, 0, 0.544376866, 0.263502832, 0, 0, 12.06101441, 0, 1.212859575, 0.30182364, 0, 0, 18.20530477, 0, 2.1561948, 0.60364728)),
class = "data.frame", row.names = c(NA, -36L))
试试这个:
library(tidyverse)
dw_wider <- structure(list(Period = c(
1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L,
2L, 2L, 2L
), DwType = c(
"1_trunks", "1_trunks", "1_trunks", "2_stumps",
"2_stumps", "2_stumps", "1_trunks", "1_trunks", "1_trunks", "2_stumps",
"2_stumps", "2_stumps"
), Plot = c(
101L, 102L, 103L, 101L, 102L,
103L, 101L, 102L, 103L, 101L, 102L, 103L
), m3ha_dc1 = c(
0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L
), m3ha_dc2 = c(
185.9886004,
36.14392074, 13.09592051, 61.99620013, 12.04797358, 4.365306836,
399.9427251, 187.306535, 59.38662, 133.3142417, 62.43551166,
19.79554
), m3ha_dc3 = c(
0.160849544, 16.04894292, 10.76337767,
0.053616515, 5.349647639, 3.587792556, 75.0880521, 27.88578286,
32.00025432, 25.0293507, 9.295260953, 10.66675144
), m2ha_dc1 = c(
0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L
), m2ha_dc2 = c(
669.9940464,
255.4172853, 147.8409739, 167.4985116, 63.85432132, 36.96024346,
1697.643681, 734.7850821, 303.60205, 424.4109202, 183.6962705,
75.9005125
), m2ha_dc3 = c(
0.40212386, 74.99540121, 87.88647158,
0.100530965, 18.7488503, 21.97161789, 297.8308267, 148.4095964,
132.5277239, 74.45770669, 37.10239909, 33.13193096
), pcsm2_dc1 = c(
0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L
), pcsm2_dc2 = c(
0.895530346,
0, 0, 0, 0.326626119, 0.065875708, 1.432848553, 0, 0, 0, 0.544376866,
0.263502832
), pcsm2_dc3 = c(
0, 0, 12.06101441, 0, 1.212859575,
0.30182364, 0, 0, 18.20530477, 0, 2.1561948, 0.60364728
)), class = "data.frame", row.names = c(
NA,
-12L
))
dw_wider |>
pivot_longer(-c(Period, Plot, DwType), names_pattern = "(.*)_dc(.+)", names_to = c("col", "dc")) |>
pivot_wider(names_from = col, values_from = value) |>
arrange(dc, Period, Plot, DwType)
#> # A tibble: 36 × 7
#> Period DwType Plot dc m3ha m2ha pcsm2
#> <int> <chr> <int> <chr> <dbl> <dbl> <dbl>
#> 1 1 1_trunks 101 1 0 0 0
#> 2 1 2_stumps 101 1 0 0 0
#> 3 1 1_trunks 102 1 0 0 0
#> 4 1 2_stumps 102 1 0 0 0
#> 5 1 1_trunks 103 1 0 0 0
#> 6 1 2_stumps 103 1 0 0 0
#> 7 2 1_trunks 101 1 0 0 0
#> 8 2 2_stumps 101 1 0 0 0
#> 9 2 1_trunks 102 1 0 0 0
#> 10 2 2_stumps 102 1 0 0 0
#> # … with 26 more rows
由 reprex package (v2.0.1)
创建于 2022-05-07