【问题标题】:looping over all combinations in a tibble循环遍历 tibble 中的所有组合
【发布时间】: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


    【解决方案1】:

    你只是想要

    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
    

    抱歉,如果我误解了这个问题。

    【讨论】:

    • 非常感谢 Kieran 提供的这个非常有效的解决方案。这正是我想要的。
    猜你喜欢
    • 2012-11-03
    • 2015-04-05
    • 2014-07-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多