I want to be able to return group_by summary, table of a variable and geom_boxplot for a dataset
Reproducible example:
set.seed(100)
df <- data.frame(Groups = rep(c("Group1","Group2","Group3"),times = 3),
AGroup = rep(c("GroupA","GroupB","GroupC"), each = 3),
Amount = sample(400:500, 9))
Printed output of the data frame
> df
Groups AGroup Amount
1 Group1 GroupA 450
2 Group2 GroupA 404
3 Group3 GroupA 474
4 Group1 GroupB 421
5 Group2 GroupB 445
6 Group3 GroupB 439
7 Group1 GroupC 418
8 Group2 GroupC 446
9 Group3 GroupC 448
The function for returning the outputs declared below:
fun1 <- function(var_name) {
table <- table(df$var_name)
summary_table <- df %>% group_by_(var_name) %>% summarise(mean(Amount))
plot <- ggplot(df, aes(var_name, Amount)) + geom_boxplot()
list(table, summary_table, plot) }
Here is the output of the function
> fun1("Groups")
[[1]]
< table of extent 0 >
[[2]]
# A tibble: 3 × 2
Groups `mean(Amount)`
<fctr> <dbl>
1 Group1 429.6667
2 Group2 431.6667
3 Group3 453.6667
[[3]]
Somewhere I have gone wrong.. I guess.. Please help