Top 'n' on column

Hi all,
I have a df like this one and what I would like to do is select only the top 2 species about their abundances in all the stations.
Like have the two species who are the more abundant

rr <- tribble(
  ~Station, ~Sp1, ~Sp2, ~Sp3, ~Sp4,
  'A1', 1, 5, 6, 4
  'A2', 3, 2, 6, 1
  'A3', 7, 0, 1, 3 
  'A4', 0, 5, 6, 7, 
  'A5', 1, 0, 6, 0,
  'A6', 0, 0, 4, 2, 
  'A7', 8, 2, 1, 7, 
  'A8', 0, 11, 1, 17, 
  'A9', 5, 2, 9, 7
)

Thanks

library(tidyverse)

rr <- tribble(
  ~Station, ~Sp1, ~Sp2, ~Sp3, ~Sp4,
  "A1", 1, 5, 6, 4,
  "A2", 3, 2, 6, 1,
  "A3", 7, 0, 1, 3,
  "A4", 0, 5, 6, 7,
  "A5", 1, 0, 6, 0,
  "A6", 0, 0, 4, 2,
  "A7", 8, 2, 1, 7,
  "A8", 0, 11, 1, 17,
  "A9", 5, 2, 9, 7
)

(rr2 <- rr |>
  summarise(across(where(is.numeric),
                   sum)) |>
  pivot_longer(cols = everything()) |>
  arrange(desc(value)))

slice_head(rr2,n=2)

Thanks a lot !
But when I do this code, I don't have my station as row names anymore
How can I keep my station names as row.names ?

You still have your original table with all the data...

You asked for the two most common species from all stations.

Answering that fact and including the species count from which it was arrived it as all i can reasonably do without more information, or a detailed specification for how the results should be presented if its going to be different to the natural result.

How would we associate 9 station names to that ? Would it provide any use ? It seems like you are asking for something that doesnt make sense, but perhaps you can explain.

Sorry I expressed myself badly, I want to make a selection to keep only the columns of the two most abundant species.
Like for this example, Sp3 and Sp4 are the more abundant species so I just want to keep these two and don't have the others ones
And have the df like this :

rr <- tribble(
  ~Station, , ~Sp3, ~Sp4,
  'A1', 6, 4
  'A2', 6, 1
  'A3', 1, 3 
  'A4', 6, 7, 
  'A5', 6, 0,
  'A6', 4, 2, 
  'A7', 1, 7, 
  'A8', 1, 17, 
  'A9', 9, 7
)

Where I just have my" top 2" species
Is that possible to do ?

Thanks for your time

you can extend the existing solution with this additional code:

(top_2_spec <- slice_head(rr2,n=2))

(rr3 <- select(rr,
              Station,all_of(sort(top_2_spec$name))))

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.