I am trying to write a function that summarises two metrics based on Sepal.Length
and Sepal.Year
, this was my attempt. Am I going about it the wrong way?
df = structure(list(Sepal.Length = c("short","short", "long", "short"),
Sepal.Width = c("wide", "narrow", "wide", "narrow"),
Petal.Length = c(1.4, 1.8, 1.3, 1.5),
Petal.Width = c(0.4, 0.7, 0.5, 0.2),
Species = structure(c(1L, 1L, 1L, 1L),
.Label = c("setosa","versicolor", "virginica"), class = "factor")),
.Names = c("Sepal.Length", "Sepal.Width", "Petal.Length", "Petal.Width", "Species"),
row.names = c(NA, 4L), class = "data.frame")
mysummary <- function(df, Criteria1, Criteria2, Metric1, Metric2){
df %>%
group_by(.dots=c(Criteria1,Criteria2)) %>%
summarise_each(funs(mean, n()),Metric1,Metric2) %>%
filter(paste(Metric1,"_n") > 3) %>%
arrange(desc(paste(Metric1,"_mean")))}
mysummary(df,"Sepal.Length","Sepal.Width","Petal.Length","Petal.Width")
The results would be: Short Wide 1.4 0.4 Short Narrow 1.65 0.45 Long Wide 1.3 0.5