0

I'm kinda new to R and I have a probably easy-to-solve question:

Here's my df:

site = c('a', 'a', 'a', 'b', 'b', 'b', 'b', 'b', 'c', 'c', 'c', 'c')
N = c(5, 4, 2, 5, 10, 15, 6, 4, 29, 14, 10, 12)
df = data.frame(site, N)

What I want to do is aggregate the sum of N by site and add the new aggregated value to a new column (df$N_total_site, such as that, for example, rows that have a will have the value 11 (the aggregated value of sites a) in df$N_total_site

So, my new column, df$N_total_site has the same numbers of row as df and the aggregated value repeats throughout the rows used to aggregate it

What I want:

site = c('a', 'a', 'a', 'b', 'b', 'b', 'b', 'b', 'c', 'c', 'c', 'c')
N = c(5, 4, 2, 5, 10, 15, 6, 4, 29, 14, 10, 12)
N_total_site = c(11, 11, 11, 40, 40, 40, 40, 40, 65, 65, 65, 65)
df = data.frame(site, N)

Thanks guys

Henrik
  • 65,555
  • 14
  • 143
  • 159
hiperhiper
  • 331
  • 1
  • 9

0 Answers0