vendredi 26 juin 2020

Create a new column with values based on conditions

I would need to create a column with values based on a third column. Example:

df <- data.frame(antibodies = c("positive","positive","positive","positive",
                            "negative","negative","negative","negative",
                            "negative","positive","positive","negative"),
             AA = c(123, 345, 7567, 234, 8679, 890, 
                    812, 435345, 567, 568, 786, 678),
             stringsAsFactors = F)

I want to create a new column named df$BB, to make this column I want the following 2 conditions:

  • If antibodies is positive then BB= AA+2
  • If antibodies is negative then BB== AA

How can I express this in R?

Thank you!

Aucun commentaire:

Enregistrer un commentaire