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!!