Reputation: 111
I would like to create an index based on n for each combination of LZCODE(data below) and Bereich over 4 years and then rbind those indices in order to plot them later. As the example below shows, I can do that by hand. However that process is quite tedious and would take a lot of time. Hence, I am looking for a loop solution based on that code.
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)
Thanks fo your help.
Data:
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"))
Upvotes: 0
Views: 43
Reputation: 1223
Do you just want
trail2 %>%
group_by(Bereich, LZCODE) %>%
mutate(Jahr = as.integer(Jahr)) %>%
arrange(Jahr, .by_group = TRUE) %>%
mutate(index = 100*n/first(n))
#> # A tibble: 50 x 7
#> # Groups: Bereich, LZCODE [14]
#> Jahr Bereich LZCODE n Bezeichnung Bezeichnung_trim index
#> <int> <dbl> <chr> <int> <chr> <chr> <dbl>
#> 1 1985 41 31 346887 Ackerland Ackerland 100
#> 2 1997 41 31 337676 Ackerland Ackerland 97.3
#> 3 2009 41 31 318685 Ackerland Ackerland 91.9
#> 4 2018 41 31 306823 Ackerland Ackerland 88.5
#> 5 1985 41 41 56588 Ackerland Ackerland 100
#> 6 1997 41 41 56517 Ackerland Ackerland 99.9
#> 7 2009 41 41 53842 Ackerland Ackerland 95.1
#> 8 1985 42 31 93322 Naturwiesen Naturwiesen 100
#> 9 1997 42 31 82756 Naturwiesen Naturwiesen 88.7
#> 10 2009 42 31 79102 Naturwiesen Naturwiesen 84.8
#> # … with 40 more rows
Apologies if I've misunderstood the question.
Upvotes: 1