0

I learn R and the tidyverse and I can not find an answer to my question because I lack the proper vocabulary to describe what I want to achieve.

I have a large dataset with that structure : df <- tibble(x = c("species1", "species2", "species1", "species3"), y = 1:4)

and I need to "compress" it into that modified dataframe by adding the observations for each unique species

df_modif <- tibble(x = c("species1", "species2", "species3"), y = c(4,2,4))

Could someone help me by explaining if there is a way to achieve this, please? And is there a word to describe that "compression" of a dataframe?

Have a nice day

Indy
  • 21
  • 2
  • 2
    `df %>% count(x, wt = y, name = 'y')` or `df %>% group_by(x) %>% summarise(y = sum(y))` – Darren Tsai Aug 08 '22 at 09:34
  • Thank you very much. So, it is the function wt or summarise that are needed. – Indy Aug 08 '22 at 09:41
  • 2
    `mutate` does not change number of rows. `summarize` does change the number of rows, usually returning 1 row per group (though that's a little flexible in recent dplyr versions.) – Gregor Thomas Aug 08 '22 at 13:20

0 Answers0