This is an example data with what I am working with:
tibble::tribble(
~item, ~quantity,
"saw", 2L,
"saw", 4L,
"saw", 6L,
"hammer", 1L,
"hammer", 2L,
"hammer", 3L
)
I want to calculate mean for each item and create a new column that shows it
this is what I am trying to get:
tibble::tribble(
~item, ~quantity, ~mean,
"saw", 2L, 4L,
"saw", 4L, 4L,
"saw", 6L, 4L,
"hammer", 1L, 2L,
"hammer", 2L, 2L,
"hammer", 3L, 2L
)
I tried this:
df <- df %>%
group_by(item) %>%
mutate(
mean = mean(quantity)
) %>%
ungroup()
but something is not working