-2

Hei,

I am trying to get the minimal value of a column (ex: maximumDepthInMeters) corresponding for each different species available of the column NameSize, and I am interested of the result; the value of the column CarbonBiomass, per locationID and per expedition. (see example of my data below)

I have been trying this code from this post (Extract row corresponding to minimum value of a variable by group)

library(data.table)
DT <- data.table(depthint_5sp_Diatoms)
min_of_maxdepth <- DT[ , .SD[maximumDepthInMeters == min(maximumDepthInMeters)], by = NameSize]

But it doesnt work for what I want.

I have also tried : (not working, get of course same table)

min_value <- depthint_5sp_Diatoms %>% 
  group_by(NameSize, CarbonBiomass, locationID, expedition) %>% 
  slice(which.min(maximumDepthInMeters))

Example of my data: (I have more locationID (ex: P2,P3, etc) and more expedition (ex: 2019706))

Grg Lulu
  • 11
  • 5

1 Answers1

0

I would try using filter instead of slice:

min_value <- depthint_5sp_Diatoms %>% 
  group_by(NameSize, locationID, expedition) %>% 
  filter(maximumDepthInMeters == min(maximumDepthInMeters))

It sounds like maybe you don't want to be grouping by CarbonBiomass but I can't tell for sure based on your description

Matt Kaye
  • 521
  • 4
  • 5
  • yes it works...thanks you! I don't want group_by CarbonBiomass but I wanted in the result, stupid me! – Grg Lulu Mar 17 '21 at 22:19