'dataframe - how to perform actions on grouped dataframe

I have the following dataframe morphology:

  month site  depth num.core num.plant num.leaf
  <chr> <chr> <dbl>    <dbl>     <dbl>    <dbl>
1 Oct   SB       12        1         1        5
2 Oct   SB       12        1         2       29
3 Oct   SB       12        1         3        7
4 Oct   SB       12        2         1        9
5 Oct   SB       12        2         2        4
6 Oct   SB       12        2         3       13

My aim if to count number of plants (num.plant) per core (num.core), at set date (month), and depth.

I have grouped the dataframe and counted the number of plants per core as I need:

morpho.group <- morphology %>%
  group_by(month, site, num.core, depth) %>%
  count(month,site,num.core,depth, name = "plant.count.Xcore") 
  month site   num.core depth plant.count.Xcore
  <chr> <chr>     <dbl> <dbl>             <int>
1 Dec   D           1     3                 4
2 Dec   D           2     3                 2
3 Dec   D           3     3                 3
4 Dec   D           4     3                 3
5 Dec   N           1    12                 1
6 Dec   N           2    12                 5

My issue is that I need to perform more actions on the morphology dataframe such as summing the number of leaves per core such as:

count.morpho <- morphology %>%
  group_by(month, site, num.core, depth) %>%
  summarise_at(vars("num.leaf", "num.roots"), sum)
 month site   num.core depth num.leaf num.roots
  <chr> <chr>     <dbl> <dbl>    <dbl>     <dbl>
1 Dec   D           1     3       11        13
2 Dec   D           2     3       17         8
3 Dec   D           3     3       14         4
4 Dec   D           4     3       40        10
5 Dec   N           1    12        3         2
6 Dec   N           2    12       40        10

I need to perform these actions such that they are continues and adds up to a single dataframe instead of pulling each calculated column to a new dataframe.

Any help is much appreciated :)



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source