R如何将多列的宽表整形为多变量的长表

wf82jlnq  于 2023-04-03  发布在  其他
关注(0)|答案(1)|浏览(118)

我的数据库包含2个测量周期在同一个监测地块。在每个监测地块上,我们测量的体积和面积的枯枝和树种更新的数量在枯枝。枯枝被分为3个分解度。我需要转换较宽的表到一个较长的。在阅读了许多帖子在这里我几乎可以肯定,解决方案是在tidyr包和pivot_longer函数。我将非常感谢你的帮助。
picture of dw_wider table and dw_longer 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))
bnlyeluc

bnlyeluc1#

试试这个:

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创建

相关问题