0

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?

1
  • 1
    The condition returns FALSE/TRUE, coded internally as 0/1. Then add 1 because R vectors are 1-based. The result indexes the vector c("DOWN", "UP"). Try doing what @Sotos said. Commented Nov 20, 2019 at 13:31

1 Answer 1

1

The first part effectively creates an object with two entries: the potential outcomes DOWN/UP. The second is a logical argument indexing one of those outcomes.

Might be easier if we create an object for the outcomes:

outcome <- c('DOWN','UP')

Square brackets index the object:

> outcome[ 2 ]
[1] "UP"

Inside the brackets is a logical statement returning FALSE/TRUE. Focusing only on the first 5 observations:

> iris[1:5,'Sepal.Length'] >= 5
[1]  TRUE FALSE FALSE FALSE  TRUE

Which store numerically as 0/1:

> as.numeric(iris[1:5,'Sepal.Length'] >= 5)
[1] 1 0 0 0 1

Addding 1 to the above returns 1s and 2s, which index either DOWN/UP in the outcomes object:

> condition[ (iris[1:5,'Sepal.Length'] >= 5) + 1 ]
[1] "UP"   "DOWN" "DOWN" "DOWN" "UP"
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.