2017-01-26 11 views
0

使用泛型函數,我試圖用summarise_all()內部函數的一個通用功能應用到數據幀的所有列。它看起來像這樣:在dplyr summarise_all

my_func <- function(df, FUN = sum, ...) { 

    < ...more stuff here ....> 

    # aggregate on desired level across all columns 
    df %>% group_by(level) %>% summarise_all(funs(FUN(., ...))) 
} 

的問題,但是,my_func,並將總是使用默認總和功能,即使我把它想:

my_func(my.data.frame, FUN="mean") # or my_func(my.data.frame, FUN=mean) 

並傳入附加PARAMS對於...論點也不起作用。

我在做什麼錯?

回答

2

一個選項是使用funs_,而不是funs

功能會再看看像

my_func <- function(df, FUN = "sum", ...) { 
    df %>% 
     group_by(level) %>% 
     summarise_all(funs_(FUN, args = ...)) 
} 

要使用args,在...參數需要被放入一個列表。

my_func(datasetname, FUN="mean", list(na.rm = TRUE)) 
+0

非常感謝! – CodingButStillAlive