【发布时间】:2021-04-28 11:10:05
【问题描述】:
我想为 LZCODE(以下数据)和 Bereich 的每个组合在 4 年内创建一个基于 n 的索引,然后对这些索引进行 rbind 以便以后绘制它们。如下例所示,我可以手动完成。然而,这个过程非常乏味,并且需要很多时间。因此,我正在寻找基于该代码的循环解决方案。
a<- trail2 %>% filter(LZCODE == 31 & Bereich == 11) %>% transform(.index=100*n/n[1])
b<- trail2 %>% filter(LZCODE == 41 & Bereich == 11) %>% transform(.index=100*n/n[1])
final<- bind_rows(a,b)
感谢您的帮助。
数据:
structure(list(Jahr = c("1985", "1997", "2009", "2018", "1985",
"2018", "1997", "1997", "2009", "2009", "2018", "1985", "1997",
"1985", "2018", "2009", "1997", "1985", "2009", "2018", "1997",
"2009", "2018", "1985", "1985", "1997", "1985", "1997", "1985",
"2018", "1985", "2009", "2018", "2009", "1997", "1997", "2009",
"2009", "2018", "2018", "1985", "2018", "1997", "2009", "2009",
"1985", "1997", "2018", "1985", "2009"), Bereich = c(41, 41,
41, 41, 46, 50, 46, 50, 50, 46, 46, 50, 50, 50, 50, 50, 50, 50,
50, 50, 50, 50, 50, 42, 50, 50, 50, 50, 50, 50, 42, 50, 50, 50,
42, 42, 42, 42, 42, 42, 42, 43, 42, 43, 42, 41, 41, 42, 42, 41
), LZCODE = c("31", "31", "31", "31", "61", "61", "61", "61",
"61", "61", "61", "61", "31", "31", "31", "31", "52", "52", "52",
"52", "53", "53", "53", "31", "53", "41", "41", "51", "51", "41",
"52", "51", "51", "41", "31", "52", "52", "31", "52", "31", "51",
"31", "51", "31", "51", "41", "41", "51", "41", "41"), n = c(346887L,
337676L, 318685L, 306823L, 211663L, 208646L, 206245L, 205096L,
204536L, 203806L, 198548L, 197565L, 186488L, 184819L, 182115L,
169676L, 139706L, 138860L, 135337L, 134505L, 95389L, 94861L,
94638L, 93322L, 92285L, 89329L, 88517L, 87410L, 86739L, 86506L,
83848L, 83366L, 83361L, 83249L, 82756L, 81789L, 80460L, 79102L,
78429L, 77593L, 62154L, 61167L, 59448L, 58686L, 57644L, 56588L,
56517L, 55279L, 54327L, 53842L), Bezeichnung = c("Ackerland",
"Ackerland", "Ackerland", "Ackerland", "Günstige Alp- und Juraweiden",
"Normalwald", "Günstige Alp- und Juraweiden", "Normalwald", "Normalwald",
"Günstige Alp- und Juraweiden", "Günstige Alp- und Juraweiden",
"Normalwald", "Normalwald", "Normalwald", "Normalwald", "Normalwald",
"Normalwald", "Normalwald", "Normalwald", "Normalwald", "Normalwald",
"Normalwald", "Normalwald", "Naturwiesen", "Normalwald", "Normalwald",
"Normalwald", "Normalwald", "Normalwald", "Normalwald", "Naturwiesen",
"Normalwald", "Normalwald", "Normalwald", "Naturwiesen", "Naturwiesen",
"Naturwiesen", "Naturwiesen", "Naturwiesen", "Naturwiesen", "Naturwiesen",
"Heimweiden", "Naturwiesen", "Heimweiden", "Naturwiesen", "Ackerland",
"Ackerland", "Naturwiesen", "Naturwiesen", "Ackerland"), Bezeichnung_trim = c("Ackerland",
"Ackerland", "Ackerland", "Ackerland", "Günstige\nAlp- und\nJuraweiden",
"Normalwald", "Günstige\nAlp- und\nJuraweiden", "Normalwald",
"Normalwald", "Günstige\nAlp- und\nJuraweiden", "Günstige\nAlp- und\nJuraweiden",
"Normalwald", "Normalwald", "Normalwald", "Normalwald", "Normalwald",
"Normalwald", "Normalwald", "Normalwald", "Normalwald", "Normalwald",
"Normalwald", "Normalwald", "Naturwiesen", "Normalwald", "Normalwald",
"Normalwald", "Normalwald", "Normalwald", "Normalwald", "Naturwiesen",
"Normalwald", "Normalwald", "Normalwald", "Naturwiesen", "Naturwiesen",
"Naturwiesen", "Naturwiesen", "Naturwiesen", "Naturwiesen", "Naturwiesen",
"Heimweiden", "Naturwiesen", "Heimweiden", "Naturwiesen", "Ackerland",
"Ackerland", "Naturwiesen", "Naturwiesen", "Ackerland")), row.names = c(NA,
-50L), groups = structure(list(Jahr = c("1985", "1985", "1985",
"1985", "1997", "1997", "1997", "1997", "2009", "2009", "2009",
"2009", "2009", "2018", "2018", "2018", "2018", "2018"), Bereich = c(41,
42, 46, 50, 41, 42, 46, 50, 41, 42, 43, 46, 50, 41, 42, 43, 46,
50), .rows = structure(list(c(1L, 46L), c(24L, 31L, 41L, 49L),
5L, c(12L, 14L, 18L, 25L, 27L, 29L), c(2L, 47L), c(35L, 36L,
43L), 7L, c(8L, 13L, 17L, 21L, 26L, 28L), c(3L, 50L), c(37L,
38L, 45L), 44L, 10L, c(9L, 16L, 19L, 22L, 32L, 34L), 4L,
c(39L, 40L, 48L), 42L, 11L, c(6L, 15L, 20L, 23L, 30L, 33L
)), ptype = integer(0), class = c("vctrs_list_of", "vctrs_vctr",
"list"))), row.names = c(NA, -18L), class = c("tbl_df", "tbl",
"data.frame"), .drop = TRUE), class = c("grouped_df", "tbl_df",
"tbl", "data.frame"))
【问题讨论】:
标签: r loops combinations