2

I have a boolean variable (e.g. x <- c(T, T, F, T, F,...)) and I want to create a second variable based on my boolean variable. If x == T, then my new variable should take value "A", otherwise, it should take a value of "B". I know it seems easy, but I am new to this.

1
  • 2
    This is a traditional if...else... question. Please see a guide on r and search for if...else... and you will find many examples and explanations on how to do what you want. Commented Nov 21, 2017 at 17:29

2 Answers 2

1

To create a variable ybased on the boolean vector xuse:

y <- ifelse(x == T, "A", "B")

The first argument of ifelse is the test, i.e. if x == TRUE, the second argument is what should be done if x == TRUE, the third argument is what should be done if x == FALSE.

Sign up to request clarification or add additional context in comments.

Comments

0

Try this

q <- ifelse(x, "A", "B")

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.