I have a dataframe "df" of animal feeding observations with ~50,000 rows. Each row is a unique food item eaten on a day of study.
The pertinent variables include:
"food_item" (an ID variable)
"day"
"month"
"year"
"time_feed" (total feeding time on the item per day)
"calories" (calories derived from each item per day)
"month_cals_total" (total monthly calories from all items)
"month_time_total" (total monthly feeding time total on all food items)
I want to find the percentage of total monthly calories and total monthly feeding time that each item contributes.
I could do this manually for each unique food item with
df %>%
group_by(month) %>%
filter(food_item == "item.1") %>%
mutate(percent_monthly_calories = sum(calories) / month_cals_total) * 100,
percent_monthly_time = sum(feed_time) / month_time_total) * 100)
and then repeat this procedure for food all food items, "item.2" through "item.150".
Desired output would be a dataframe retaining rows and columns but with two new columns added "percent_monthly_time" and "percent_monthly_calories" that list the percentage of each food item to monthly feeding time and monthly calories, respectively.
Any suggestions (preferably a tidyverse solution) about how to embed a for
loop or an apply
function within this so I can calculate the percentage of each food item to total monthly feeding time at once?