2017-06-22 32 views
0

當更新到最新版本的包dplyr時,我已經使用NSE的功能被破壞了。我想知道新版本如何改變這個以及如何解決它。我試過在每個變量名前使用.data$.env$,但似乎無法使其工作。在新的dplyr版本中打破NSE功能

這裏是我的自定義函數:

t_ppond <- function(w, v){ 
    arguments <- as.list(match.call()) 

    y <- eval(arguments$w) 
    x <- eval(arguments$v) 

    d <- data.frame("yy" = y, 
        "xx" = x) 
    tt <- sum(d$yy) 

    dff <- d %>% 
     mutate("sh" = yy/tt) %>% 
     mutate("rr" = xx*sh) 

    sum(dff$rr) 
} 

而這就是我用了(從變量計算加權平均):

data(iris) 
iris %>% 
    group_by(Species) %>% 
    summarise("new" = t_ppond(Sepal.Length, Petal.Width)) 

上面的代碼更新之前的工作完美。現在我得到:

Error in summarise_impl(.data, dots) : 
    Evaluation error: object 'Sepal.Length' not found. 

回答

1

您確定需要非標準評估嗎?您有隻需要採取兩個向量自定義功能,讓你可以寫這樣的:

t_ppond <- function(w, v){ 
    d <- data.frame("yy" = w, 
        "xx" = v) 
    tt <- sum(d$yy) 

    dff <- d %>% 
     mutate("sh" = yy/tt) %>% 
     mutate("rr" = xx*sh) 

    sum(dff$rr) 
} 

data(iris) 
iris %>% 
    group_by(Species) %>% 
    summarise("new" = t_ppond(Sepal.Length, Petal.Width)) 

輸出:

# A tibble: 3 x 2 
    Species  new 
     <fctr>  <dbl> 
1  setosa 0.2480224 
2 versicolor 1.3352089 
3 virginica 2.0333030 
+0

你是對的!我陷入了試圖讓它工作的原因!謝謝! – eflores89