【问题标题】:Conditional Evaluation in DplyrDplyr 中的条件评估
【发布时间】:2021-06-17 18:46:19
【问题描述】:

我有一个字符向量r <- c()。我想根据 r 的长度对数据帧进行变异

这行得通

iris %>% if(length(r) > 0) mutate(Test = 1) else .

当我扩展以添加更多 dplyr 动词时,这不起作用

iris %>% if(length(r) > 0) mutate(Test = 1) else . %>% mutate(Test2 = 1)

我只是在寻找基于 dplyr 的解决方案。

【问题讨论】:

    标签: r dplyr


    【解决方案1】:

    由于有多个语句,请将其包装在 {}

    r <- c()
    iris %>%
        {if(length(r) > 0) {
            mutate(., Test = 1)
          } else .}
        Sepal.Length Sepal.Width Petal.Length Petal.Width    Species
    1            5.1         3.5          1.4         0.2     setosa
    2            4.9         3.0          1.4         0.2     setosa
    3            4.7         3.2          1.3         0.2     setosa
    4            4.6         3.1          1.5         0.2     setosa
    ...
    

    -使用r长度> 0进行测试

    r <- 5
    iris %>%
         {if(length(r) > 0) {
             mutate(., Test = 1)
           } else .}
        Sepal.Length Sepal.Width Petal.Length Petal.Width    Species Test
    1            5.1         3.5          1.4         0.2     setosa    1
    2            4.9         3.0          1.4         0.2     setosa    1
    3            4.7         3.2          1.3         0.2     setosa    1
    ...
    

    但是,这可以在没有循环的情况下轻松修改,即通过添加 1 将逻辑向量转换为数字索引(因为R 中的索引从 1 开始)。使用它来选择值为 1 和 NULL 的 list。如果长度为 0,则选择 NULL,因此不创建列

    iris %>%
        mutate(Test =  list(NULL, 1)[[1 + (length(r) > 0)]])
    

    【讨论】:

      【解决方案2】:
      library(dplyr)
      

      一旦被匿名函数替换,使用中间函数提供了另一种解决方案

      g_if <- function(df, r){
        if(length(r)) {
          ans <- df %>% mutate(test = 1)
        } else {
          ans <- df
        }
        invisible(ans)
      }
      
      r <- c()
      iris %>% g_if(r) %>% str
      #> 'data.frame':    150 obs. of  5 variables:
      #>  $ Sepal.Length: num  5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
      #>  $ Sepal.Width : num  3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
      #>  $ Petal.Length: num  1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
      #>  $ Petal.Width : num  0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
      #>  $ Species     : Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1 1 1 1 1 1 1 ...
      
      r <- c(1)
      iris %>% g_if(r) %>% str
      #> 'data.frame':    150 obs. of  6 variables:
      #>  $ Sepal.Length: num  5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
      #>  $ Sepal.Width : num  3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
      #>  $ Petal.Length: num  1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
      #>  $ Petal.Width : num  0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
      #>  $ Species     : Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1 1 1 1 1 1 1 ...
      #>  $ test        : num  1 1 1 1 1 1 1 1 1 1 ...
      

      现在,我们可以对匿名函数使用相同的想法,即无需显式定义 函数g_if()

      r <- c()
      iris %>% {
        function(df, cond){
          if(length(cond) > 0) {
            ans <- df %>% mutate(test = 1)
          } else {
            ans <- df
          }
          ans}}(r) %>%
      head
      #>   Sepal.Length Sepal.Width Petal.Length Petal.Width Species
      #> 1          5.1         3.5          1.4         0.2  setosa
      #> 2          4.9         3.0          1.4         0.2  setosa
      #> 3          4.7         3.2          1.3         0.2  setosa
      #> 4          4.6         3.1          1.5         0.2  setosa
      #> 5          5.0         3.6          1.4         0.2  setosa
      #> 6          5.4         3.9          1.7         0.4  setosa
      
      r <- c(1)
      iris %>% {
        function(df, cond){
          if(length(cond) > 0) {
            ans <- df %>% mutate(test = 1)
          } else {
            ans <- df
          }
          ans}}(r) %>%
      head
      #>   Sepal.Length Sepal.Width Petal.Length Petal.Width Species test
      #> 1          5.1         3.5          1.4         0.2  setosa    1
      #> 2          4.9         3.0          1.4         0.2  setosa    1
      #> 3          4.7         3.2          1.3         0.2  setosa    1
      #> 4          4.6         3.1          1.5         0.2  setosa    1
      #> 5          5.0         3.6          1.4         0.2  setosa    1
      #> 6          5.4         3.9          1.7         0.4  setosa    1
      

      reprex package (v0.3.0) 于 2021-06-17 创建

      【讨论】:

        【解决方案3】:

        我们可以使用ifelse

        library(dplyr)
        r <- c()
        iris %>% 
          mutate(Test = ifelse(length(r) > 0, 1,1))
        

        输出:

          Sepal.Length Sepal.Width Petal.Length Petal.Width Species Test
        1          5.1         3.5          1.4         0.2  setosa    1
        2          4.9         3.0          1.4         0.2  setosa    1
        3          4.7         3.2          1.3         0.2  setosa    1
        4          4.6         3.1          1.5         0.2  setosa    1
        5          5.0         3.6          1.4         0.2  setosa    1
        6          5.4         3.9          1.7         0.4  setosa    1
        

        【讨论】:

        • 即使length(r) &gt; 0FALSE,此答案也会创建Test 列。我不认为这是目标。如果FALSE,则不要添加Test 列。
        【解决方案4】:

        如果满足条件,下面的代码将添加变量。如果不是,它将添加一个填充的变量将全部 NA 并最终将其删除(我知道您只有在满足条件时才需要新变量)。

        library(dplyr)
        
        r <- c()
        
        iris %>% 
          mutate(test2=if_else(length(r)>0, 2, NULL)) %>% 
          select(where(~ !(all(is.na(.))))) #remove columns with all NAs 
        

        【讨论】:

        • 我认为这是使用if_else/ifelse 的错误方式,因为Values to use for TRUE and FALSE values of condition. They must be either the same length as condition, or length 1.NULL 没有长度,因此您得到NA
        猜你喜欢
        • 2018-05-31
        • 2018-11-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-10-02
        • 1970-01-01
        • 2015-03-23
        相关资源
        最近更新 更多