0

I'm in desperate need of some guidance on how to write a function

This is some sample data:

df <- data.frame(
  group = c("U7","U8","U8","U7","U7","U7","U7","U9","U9","U9"),
  value = rnorm(10)
)

This is what I now do: copy paste the same formula...

Median_U7 <- df %>% filter(group=="U7") %>% summarise(median=median(value, na.rm = TRUE))
Median_U8 <- df %>% filter(group=="U8") %>% summarise(median=median(value, na.rm = TRUE))
Median_U9 <- df %>% filter(group=="U9") %>% summarise(median=median(value, na.rm = TRUE))

I know I have to write a function with 2 arguments, df & x (which will be the group), but I can't quite figure out how to make it work

This doens't work:

my_fun <- function(df,x) {

  Median_x <- df %>% filter(group=="x") %>% summarise(median=median(value, na.rm = TRUE))
  Median
}


my_fun(df,"U7")
  • there should be a for loop included to loop over the group's automatically...

Help very much appreciated!!

Sotos
  • 51,121
  • 6
  • 32
  • 66
  • 1
    use `dplyr::group_by(group)` instead of filtering, then each group is summarized separately. – Thomas K Aug 23 '18 at 13:09
  • Definitely sounds like a case for `group_by`; in your case `df %>% group_by(group) %>% summarise(median = median(value, na.rm = T))` – Maurits Evers Aug 23 '18 at 13:10
  • So like this then ?median_by_team <- function(df) { df %>% group_by(group) %>% summarise(median=median(value, na.rm = TRUE)) } Median <- median_by_team(df) – Wouter Bollaert Aug 23 '18 at 13:23

0 Answers0