If statement - selection

Ive got a big dataset im doing my sampling from. For each sample, Im creating a dataframe.
The dataframe consists of 3 columns. "Species code" "Diameter" "Height". Species code range from 1-4.

Based on what species my sample consists of, I want to make a model.
This is what I want the code to do:

If (Number of observations/rows of speciescode 1 is equal to 3 or higher AND number of observations/rows of speciescode 2,3 and 4 is equal to 3 or higher)
Make model for speciescode 1
Make model for species code 2,3 and 4 together.
Else (If the above statement is not true)
Make a universal model

Does anyone know how a code like this would look like? Creating the models is no problem.


Something like this? AllData is the original data.

Num_1 <- AllData |> filter(SpeciesCode == 1) |> nrow()
Num_234 <- nrow(AllData) - Num_1
if(Num_1 >= 3 & Num_234 >=3) {
  #Fit models for 1 and (2,3,4)
} else {
  #Fit model (1,2,3,4)

Solved! Thank you for the help :slight_smile:

This topic was automatically closed 21 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.