【问题标题】:Grouping arguments in tidyverse: Bin by consecutive number of levels in a variable with missing levels在 tidyverse 中对参数进行分组:按缺少级别的变量中的连续级别数进行分类
【发布时间】:2017-10-14 05:02:04
【问题描述】:

我有一个数据框,其中包含每月数据 month 关于草食动物寄生 result 的各种类型的寄生蜂命令“psitorder”,级别为“膜翅目”或“双翅目”。结果要么是寄生的草食动物的“p”,如果草食动物长大成人,则为“a”,或者没有数据“”,因为草食动物在圈养中死亡。

 df<-data.frame(month= c(rep(1, each=8), rep(2, each=6), 
                    rep(3, each=6), rep(4, each=8), 
                    rep(5,each=6),rep(6, each=6), 
                    rep(8,each=6),rep(9, each=6)),
                result= c(rep("p",each=3),rep("a",each=3), 
                     rep("",each=2),rep("p",each=3),rep("a",each=2), 
                     rep("",each=1),rep("a",each=3),rep("",each=3),
                     rep("p",each=3),rep("a",each=3),rep("",each=2),
                     rep("p",each=3),rep("a",each=2), 
                     rep("",each=1),rep("a",each=3),rep("",each=3),
                     rep("p",each=3),rep("a",each=3),rep("",each=2),         
                     rep("a",each=4)),
                 psitorder=c(rep("Hymenoptera",each=2),
                     rep("Diptera",each=1),rep("",each=5),
                     rep("Hymenoptera",each=1),rep("Diptera",each=3),
                     rep("",each=2),rep("",each=6),
                     rep("Hymenoptera",each=2),rep("Diptera",each=1),
                     rep("",each=5),rep("Hymenoptera",each=1),
                     rep("Diptera",each=3),rep("",each=2),
                     rep("",each=6),rep("Hymenoptera",each=2), 
                     rep("Diptera",each=1),rep("",each=9)))

我想按month 变量分组,但是,我需要按每连续 3 个月的数据分组。在这里,在此示例中,month 1,2,3 将被分组,月份为 4,5,6,对于月份 7,9,我需要添加缺少的月份 8,以便继续使用连续行来计算psit_freq

分组后,我想使用以下方法计算psit_freq

我试过了:

output %>% 
group_by(month+3) %>% 
mutate(complete(continuous_month= seq(min(continuous_month), 
max(continuous_month), 1L))%>%
summarise(hym_freq = sum(psitorder == 'Hymenoptera')/sum(result %in% c('p', 'a')), 
          dip_freq = sum(psitorder == 'Diptera')/sum(result %in% c('p', 'a')))

输出如下所示:

output<- data.frame(group= c("1", "2", "3"), hym_psit= c(3/14, 
         3/14,2/10), dip_psit= c(4/14,4/14,1/10))

【问题讨论】:

    标签: r tidyverse


    【解决方案1】:

    我们用%/%创建一个分组变量

    data_frame(month = 1:9) %>% 
           full_join(., df) %>% 
           group_by(group = (month-1)%/%3 + 1) %>%
          summarise(hym_freq = sum(psitorder == 'Hymenoptera', na.rm = TRUE)/sum(result %in% c('p', 'a'), na.rm = TRUE), 
               dip_freq = sum(psitorder == 'Diptera', na.rm =TRUE)/sum(result %in% c('p', 'a'), na.rm = TRUE))
    # A tibble: 3 x 3
    #   group  hym_freq  dip_freq    
    #    <dbl>     <dbl>     <dbl>
    #1     1 0.2142857 0.2857143
    #2     2 0.2142857 0.2857143
    #3     3 0.2000000 0.1000000
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-10-11
      • 2019-12-28
      • 1970-01-01
      • 1970-01-01
      • 2021-01-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多