【问题标题】:dplyr: function error messagedplyr:函数错误信息
【发布时间】:2017-06-13 21:36:17
【问题描述】:

我创建了一个函数来根据 id 减去我的一些数据。在dplyr 更新之前,该功能运行良好。最初,该函数不接受列名作为函数的输入。我使用Programming with dplyr 调整函数以接受列名,但是我现在收到一条新的错误消息。

testdf <- structure(list(date = c("2016-04-04", "2016-04-04", "2016-04-04", 
                        "2016-04-04", "2016-04-04", "2016-04-04"), sensorheight = c(1L, 
                                                                                    16L, 1L, 16L, 1L, 16L), farm = c("McDonald", "McDonald", 
                                                                                                                     "McDonald", "McDonald", "McDonald", "McDonald"
                                                                                    ), location = c("4", "4", "5", "5", "Outside", "Outside"), Temp = c(122.8875, 
                                                                                                                                                        117.225, 102.0375, 98.3625, 88.5125, 94.7)), .Names = c("date", 
                                                                                                                                                                                                                "sensorheight", "farm", "location", "Temp"), row.names = c(NA, 
                                                                                                                                                                                                                                                                           6L), class = "data.frame")


DailyInOutDiff <- function (df, variable) {

  DailyInOutDiff04 <- df %>%
    filter(location %in% c(4, 'Outside')) %>% 
    group_by(date, sensorheight, farm) %>%
    arrange(sensorheight, farm, location) %>%
    summarise(Diff = if(n()==1) NA else !!variable[location=="4"] - !!variable[location=='Outside'], 
              location = "4")  %>%
    select(1, 2, 3, 5, 4)

  DailyInOutDiff05 <- df %>%
    filter(location %in% c(5, 'Outside')) %>% 
    group_by(date, sensorheight, farm) %>%
    arrange(sensorheight, farm, location) %>%
    summarise(Diff = if(n()==1) NA else !!variable[location=="5"] - !!variable[location=='Outside'], 
              location = "5")  %>%
    select(1, 2, 3, 5, 4)

  temp.list <- list(DailyInOutDiff04, DailyInOutDiff05)
  final.df = bind_rows(temp.list)
  return(final.df)
}

test <- DailyInOutDiff(testdf, quo(Temp))

我想知道错误消息的含义以及如何修复它。

 Error in location == "4" : 
    comparison (1) is possible only for atomic and list types 

【问题讨论】:

    标签: r dplyr


    【解决方案1】:

    我认为! 的优先级导致了问题。发生这种情况时,看起来应该使用 UQ 代替 !!

    在这种情况下,你的函数的第一部分看起来像

    DailyInOutDiff <- function (df, variable) {
    
        variable = enquo(variable)
    
        df %>%
            filter(location %in% c(4, 'Outside')) %>% 
            group_by(date, sensorheight, farm) %>%
            arrange(sensorheight, farm, location) %>%
            summarise(Diff = if(n()==1) NA else UQ(variable)[location == "4"] - 
                        UQ(variable)[location == "Outside"], 
                    location = "4")
    
    }
    

    现在运行没有错误。

    DailyInOutDiff(testdf, Temp)
    
            date sensorheight     farm   Diff location
           <chr>        <int>    <chr>  <dbl>    <chr>
    1 2016-04-04            1 McDonald 34.375        4
    2 2016-04-04           16 McDonald 22.525        4
    

    我认为使用UQ 可能是最好的方法。另一种选择是以函数的形式使用提取括号。这也绕过了优先级问题。

    例如,看起来像这样的代码

    !!variable[location == "4"]
    

    可以改写为

    `[`(!!variable, location == "4")
    

    对函数的第一部分进行这些更改,看起来像

    DailyInOutDiff <- function (df, variable) {
    
        variable = enquo(variable)
    
        df %>%
            filter(location %in% c(4, 'Outside')) %>% 
            group_by(date, sensorheight, farm) %>%
            arrange(sensorheight, farm, location) %>%
            summarise(Diff = if(n()==1) NA else `[`(!!variable, location == "4") - 
                        `[`(!!variable, location == "Outside"), 
                    location = "4")
    
    }
    

    也可以正常运行

    DailyInOutDiff(testdf, Temp)
    
            date sensorheight     farm   Diff location
           <chr>        <int>    <chr>  <dbl>    <chr>
    1 2016-04-04            1 McDonald 34.375        4
    2 2016-04-04           16 McDonald 22.525        4
    

    【讨论】:

    • UQ 效果很好,我喜欢在函数中包含enquo。谢谢。
    猜你喜欢
    • 1970-01-01
    • 2015-07-21
    • 2015-01-11
    • 2018-05-12
    • 2021-10-01
    • 2015-03-28
    • 2016-04-07
    • 2011-01-07
    • 1970-01-01
    相关资源
    最近更新 更多