'R - sum a variable across multiple groups within a column subset by second group in another column

I've been looking for an example of my question but haven't been able to find it. Here is a simplification of my data:

plot_id <- c("plot_1","plot_1","plot_1","plot_1","plot_2","plot_2","plot_2","plot_2")
size_class <- c("1","2","3","4","1","2","3","4")
weight <- c(1.05,11.06,17.48,131.76,0.23, 8.38, 3.30,69.58)
df <- data.frame(plot_id,size_class,weight)
  plot_id size_class weight
1  plot_1          1   1.05
2  plot_1          2  11.06
3  plot_1          3  17.48
4  plot_1          4 131.76
5  plot_2          1   0.23
6  plot_2          2   8.38
7  plot_2          3   3.30
8  plot_2          4  69.58

I would like to sum weight across size_class 1, 2 and 3 grouped by plot_id. The resulting sum would be a new size_class 123. So the result would look like this:

  plot_id size_class weight
1  plot_1        123  29.59
2  plot_2        123  11.91

I'd then like to the add these new observations to the original dataframe.

I'm still getting a grasp on data wrangling and I haven't been able to figure this one out, any help is much appreciated!



Solution 1:[1]

You could use

library(dplyr)

df %>% 
  filter(size_class %in% 1:3) %>% 
  group_by(plot_id) %>% 
  summarise(size_class = paste0(size_class, collapse = ""),
            weight = sum(weight)) %>% 
  bind_rows(df)

This returns

# A tibble: 10 x 3
   plot_id size_class weight
   <chr>   <chr>       <dbl>
 1 plot_1  123         29.6 
 2 plot_2  123         11.9 
 3 plot_1  1            1.05
 4 plot_1  2           11.1 
 5 plot_1  3           17.5 
 6 plot_1  4          132.  
 7 plot_2  1            0.23
 8 plot_2  2            8.38
 9 plot_2  3            3.3 
10 plot_2  4           69.6 

Sources

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

Source: Stack Overflow

Solution Source
Solution 1 Martin Gal