【问题标题】:Multiple condition if-else using dplyr, custom function, or purr使用 dplyr、自定义函数或 purr 的多个条件 if-else
【发布时间】:2019-02-01 08:51:44
【问题描述】:

我有一个类似于以下结构的数据框:

set.seed(123)  
df<-data_frame(SectionName = rep(letters[1:2], 50),
               TimeSpentSeconds = sample(0:360, 100, replace = TRUE),
               Correct = sample(0:1, 100, replace = TRUE))

我想通过将 TimeSpentSeconds 的所有值纳入特定范围(小于 30、30-60 之间、60-90 之间、...、大于 180)来总结此数据框,将时间标记为这些范围,按 SectionName 对它们进行分组,并找到正确列的总和,以便生成的数据框看起来像这样:

    TimeGroup             SectionName Correct
   <fct>                 <chr>         <int>
 1 LessThan30Secs        a                 2
 2 LessThan30Secs        b                 3
 3 30-60 Seconds         a                 4
 4 30-60 Seconds         b                 3
 5 60-90 Seconds         a                 2
 6 60-90 Seconds         b                 3
 7 90-120 Seconds        a                 4
 8 90-120 Seconds        b                 0
 9 120-150 Seconds       a                 4
10 120-150 Seconds       b                 0
11 150-180 Seconds       a                 1
12 150-180 Seconds       b                 2
13 GreaterThan180Seconds a                11
14 GreaterThan180Seconds b                11

我能够使用下面的 if-else 代码成功地做到这一点,在该代码中,我将所有时间都变成了一个带有适当标签、分组和汇总的新列:

x <- c("LessThan30Secs", "30-60 Seconds", "60-90 Seconds","90-120 Seconds", 
           "120-150 Seconds", "150-180 Seconds", "GreaterThan180Seconds") 

df %>% 
mutate(TimeGroup = if_else(TimeSpentSeconds >= 0 & TimeSpentSeconds <= 30, "LessThan30Secs",
                if_else(TimeSpentSeconds > 30 & TimeSpentSeconds <= 60, "30-60 Seconds",
                if_else(TimeSpentSeconds > 60 & TimeSpentSeconds <= 90, "60-90 Seconds",
                if_else(TimeSpentSeconds > 90 & TimeSpentSeconds <= 120, "90-120 Seconds",
                if_else(TimeSpentSeconds > 120 & TimeSpentSeconds <= 150, "120-150 Seconds", 
                if_else(TimeSpentSeconds > 150 & TimeSpentSeconds <= 180, "150-180 Seconds",
                if_else(TimeSpentSeconds > 180, "GreaterThan180Seconds", "")))))))) %>%
    mutate(TimeGroup = factor(TimeGroup, levels = x)) %>%
    arrange(TimeGroup) %>%
    group_by(TimeGroup, SectionName) %>%
    summarise(Correct = sum(Correct))

但是,必须有更好的方法来做到这一点。我考虑过写一个函数,但因为我不擅长写函数,所以没有走得太远。

是否有人对通过我没有想到的 dplyr 方法完成相同输出的更优雅方式有任何想法,编写自定义函数可能在某些时候使用 purrr 包或其他一些 r 函数?

【问题讨论】:

  • 不要使用if_else,而是尝试使用cutfindInterval,即df %&gt;% group_by(TimeGroup = cut(TimeSpentSeconds, breaks = c(seq(0, 180, by = 30), Inf)), SectionName) %&gt;% summarise(Correct = sum(Correct))
  • 最好有一个set.seed 以使其可重现
  • 虽然在这种情况下findInterval 是更好的解决方案,但如果间隔不是完全连续的,那么使用dplyr::case_when 会更合适。

标签: r dplyr purrr


【解决方案1】:

case_when() 会做你想做的事。它是嵌套 ifelse() 语句的一种简洁替代方案。

library(dplyr)

mutate(df,
       TimeGroup = case_when(
         TimeSpentSeconds <= 30 ~ "30 Seconds or less",
         TimeSpentSeconds <= 60 ~ "31-60 Seconds",
         TimeSpentSeconds <= 90 ~ "61-90 Seconds",
         TimeSpentSeconds <= 120 ~ "91-120 Seconds",
         TimeSpentSeconds <= 150 ~ "121-150 Seconds", 
         TimeSpentSeconds <= 180 ~ "151-180 Seconds",
         TimeSpentSeconds > 180 ~ "Greater Than 180 Seconds",
         TRUE ~ NA_character_)
)

最后一个参数是不符合任何条件的记录的全部捕获,例如时间是否小于 0 秒。

【讨论】:

  • 请注意所有“公式”(~ 之后)必须属于同一类型。谢谢!
  • 谢谢,我已经修复了 TRUE 案例。
  • 不需要每次都比较,已经被拒绝的语句。比如第二个语句,你说TimeSpentSeconds &gt; 30,但是为了检查,我们通过TimeSpentSeconds &lt;= 30,所以它必须大于30。
【解决方案2】:

我们可以使用cut(或findInterval)轻松做到这一点,而不是使用多个嵌套的ifelse 语句

lbls <- c('LessThan30secs', '30-60 Seconds', '60-90 Seconds', 
    '90-120 Seconds', '120-150 Seconds', '150-180 Seconds', 'GreaterThan180Seconds')
df %>% 
    group_by(TimeGroup = cut(TimeSpentSeconds, 
                 breaks = c(seq(0, 180, by = 30), Inf), labels = lbls), 
            SectionName) %>%
   summarise(Correct = sum(Correct)) %>%
   na.omit

【讨论】:

  • 这太棒了!只有一个问题。当我把它放在我的实际数据中时,我得到了两组“LessThan30secs”的不同值。经过调查,我发现此代码不包含在“LessThan30secs”组中任何具有 df$TimeSpentSeconds == 0 的内容。有没有办法使用 cut 来包含 0?
【解决方案3】:
``` r
library(tidyverse)
set.seed(123)
df<-data_frame(SectionName = rep(letters[1:2], 50),
               TimeSpentSeconds = sample(0:360, 100, replace = TRUE),
               Correct = sample(0:1, 100, replace = TRUE))

time_spent_range <- function(value, start, end, interval) {
  end <- end + (end%%interval) # make sure the end value is divisible by the interval
  bins_start <- seq(start, end - interval, by = interval)
  bins_end <- seq(start + interval, end, by = interval)
  bins_tibble <- tibble(bin_start = bins_start, 
                        bin_end = bins_end) %>% 
    mutate(in_bin = if_else((value > bin_start|(value == 0 & bin_start == 0)) 
                            & value <= bin_end,
                            1,
                            0)) %>% 
    filter(in_bin == 1)
  bin <- paste0(as.character(bins_tibble$bin_start[1]), 
                '-', 
                as.character(bins_tibble$bin_end[1]),
                ' Seconds')
  return(bin)
}

df %>% 
  mutate(TimeGroup = map_chr(TimeSpentSeconds, time_spent_range, start = 0, end = max(df$TimeSpentSeconds) , interval = 30))
#> # A tibble: 100 x 4
#>    SectionName TimeSpentSeconds Correct TimeGroup      
#>    <chr>                  <int>   <int> <chr>          
#>  1 a                        103       1 90-120 Seconds 
#>  2 b                        284       0 270-300 Seconds
#>  3 a                        147       0 120-150 Seconds
#>  4 b                        318       1 300-330 Seconds
#>  5 a                        339       0 330-360 Seconds
#>  6 b                         16       1 0-30 Seconds   
#>  7 a                        190       1 180-210 Seconds
#>  8 b                        322       1 300-330 Seconds
#>  9 a                        199       0 180-210 Seconds
#> 10 b                        164       0 150-180 Seconds
#> # ... with 90 more rows
```

reprex package (v0.2.0) 于 2018 年 8 月 26 日创建。

【讨论】:

    猜你喜欢
    • 2018-12-09
    • 2020-10-17
    • 1970-01-01
    • 2012-04-25
    • 1970-01-01
    • 2022-12-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多