Creating a population mean variable/column in an existing dataset with individual values

In a tidy data frame with multiple observations/values for levels of categorical variables, is it possible to ADD a summary statistic value (e.g. mean) as a new column/variable to the data frame? Such that this mean would be repeated for multiple rows where it is describing that grouping of factors.

I successfully calculated the summary statistics using this code and creating a new data frame. This new data frame then has fewer observations because it is combining the observations of the input tidy_data dataframe. However, I would like to add the mean values as a new column to the original tidy_data dataframe rather than creating a separate summary data frame such that each original observation is tied to its grouped mean value as well as its raw value.

sum_stats <- tidy_data %>%
group_by(factor1, factor2, factor3) %>%
# group by categorical variables
population_mean = mean(freq), # calc mean
population_sd = sd(freq), # calc standard dev

Thanks in advance for any input and help!

Simply use mutate() instead of summarise()

If you need more specific help, please provide a proper REPRoducible EXample (reprex) illustrating your issue.

Thank you!

I had tried mutate() but using the $ operator along with my data frame, and that was my error. When I simply exchange the function summarise() for mutate() it works perfectly.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.