I want to add one a column called "Opt-Numbers" to my data frame with the following values Opt-CMM and Opt-MM based on Numbers column. If the value in Numbers column are greater or equal to 4 then it should add Opt-CMM in the same row of that value or if it is less than 4 then add Opt-MM in the same row. I am also showing an example in below df.
Given DF.




S.NO
Numbers




P1
2


P2
5


P3
2


P4
2


P5
3


P6
4




Required DF




S.NO
Numbers
Opt-Numbers




P1
2
Opt-MM


P2
5
Opt-CMM


P3
2
Opt-MM


P4
2
Opt-MM


P5
3
Opt-MM


P6
4
Opt-CMM




Add a Column with a string value based on other column values R
We can do this with dplyr and case_when ()
library(dplyr)

#example data
data <- structure(list(S.NO = c("P1", "P2", "P3", "P4", "P5", "P6"), Numbers = c(2, 5, 2, 2, 3, 4)), class = "data.frame", row.names = c(NA, -6L))

create new column with filters
data <- data %>%
          mutate(Opt-Numbers = case_when(
                    Numbers >= 4 ~ "Opt-CMM",
                    Numbers < 4 ~ "Opt-MM"
          ))

data

