0

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?

Will
  • 81
  • 5
  • Please make a [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) or [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) with a sample input and your expected output. – Martin Gal Apr 29 '22 at 20:03
  • 3
    Couldn‘t you simply add food_item as a second grouping variable? – deschen Apr 29 '22 at 20:14
  • 2
    You could try `group_by(month, food_item)` and replace `mutate` with `summarise`? – bobloblawlawblog Apr 29 '22 at 20:15
  • Adding another grouping variable seems to work, though I realized that I needed another grouping variable for "year" too since the same months show up across years. `group_by(month, year, food_item)` then `summarise` seems to work. Thank you kindly – Will Apr 29 '22 at 20:49

0 Answers0