I refer to Oscar de Léon's answer to this question: How to add column into a dataframe based on condition?
This is his code:
iris$Regulation <- c("DOWN", "UP")[ (iris$Sepal.Length >= 5) + 1 ]
I understand it assigns values to the column "Regulation" and hence creates it, if it's not there yet. But then? I see it works but I don't understand what it is exactly doing. Could you explain what it does?
FALSE/TRUE, coded internally as0/1. Then add1because R vectors are 1-based. The result indexes the vectorc("DOWN", "UP"). Try doing what @Sotos said.