-1

enter image description here

I would like to create a function to classify column "Stage" based on multiple conditions as example bellow.

  1. Stage (column) = "Ovo" == Age 0 to 8 <= Temp 30 returns = "positive" if not "negative

  2. Stage (column)= "Adulto" == Age <= 5 returns = "positive" if not "negative"

  3. Stage(column)= "Lagarta" == Age <= 62 < Temp 27 returns = "positive" if not "negative"

I have multiple conditions for level factor of column (Stage). Please, could someone help me to create a function to make it simultaneously and create one new column with values "positive" or "negative"?

ThomasIsCoding
  • 96,636
  • 9
  • 24
  • 81
BD'auria
  • 135
  • 10
  • 1
    Please don't post data as images. Take a look at how to make a [great reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) for ways of showing data. – Martin Gal Aug 12 '21 at 22:30
  • Can you show what you have tried and provide a dput of some of your data? – Michael Vine Aug 12 '21 at 22:30
  • You pseudo-code conditions are also hard to read/interprete. Could you please clear them up? – Martin Gal Aug 12 '21 at 22:42

2 Answers2

3

Supposing your data.frame is named df, you could use

library(dplyr)

df %>% 
  mutate(class = ifelse(
    (Stage == "Ovo" & Age <= 8 & Age >= 0 & Temp <= 30) |
      (Stage == "Adulto" & Age <= 5) |
      (Stage == "Lagarta" & Age <= 62 & Temp < 27),
    "positive",
    "negative"
    )
  )

The case_when-function is also an option, if you want to add multiple conditions this could improve readability.

Martin Gal
  • 16,640
  • 5
  • 21
  • 39
3

We can use case_when

library(dplyr)
df1 %>%
     mutate(class = case_when(Stage == 'OVo' & between(Age, 0, 8) & Temp <=30 ~ "positive", Stage == "Adulto" & Age <=5 ~ "positive", Stage == "Lagarta" & between(Age, 27, 62) ~ "positive", TRUE ~ "negative"))
akrun
  • 874,273
  • 37
  • 540
  • 662