0

I am using the following code in R to create a new variable called success in my dataset (leaders)

leaders$success <- as.numeric(leaders$result == 'dies within a day after the attack','dies between a day and a week','dies between a week and a month','dies, timing unknown')

It happens that the code above only compute the variable dies within a day after the attack. How should a rephrase the code in order to compute the other three variables? I tried using the OR operator, | but It is possible only for numeric arguments.

Could someone help me?

Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
  • 1
    Are you looking for `%in%` instead of `==` ? Like `1 %in% c(1,2,3)`? 'Or', as `|`, is definitely not only for numeric arguments either. It will combine any logical comparison `"a" == "b" | 1 == 1` for instance. – thelatemail May 14 '20 at 03:15
  • Please include a reproducible question as suggested here [How to ask good question](https://stackoverflow.com/help/minimal-reproducible-example) and [Reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) include your data (as a dataframe object or use dput("yourdata"), the code you have tried and your expected output. This will make it more likely to get a good answer. – rj-nirbhay May 14 '20 at 03:29
  • when I use ```leaders$success <- as.numeric(leaders$result == "dies within a day after the attack" | "dies between a day and a week" | "dies between a week and a month" | "dies, timing unknown")``` It shows the following error: "operations are possible only for numeric, logical or complex types" – Andre Masuko May 14 '20 at 03:30
  • 1
    `leaders$success <- as.numeric(leaders$result %in% c('dies within a day after the attack','dies between a day and a week','dies between a week and a month','dies, timing unknown'))` – Ronak Shah May 14 '20 at 03:35
  • Yep, to provide further info: you are unintentionally doing `"string" | "other string"`. However, `|` and `&` are for making `logical` comparisons like `TRUE | FALSE` and `TRUE & TRUE`. Character strings have no way of being considered as `TRUE` or `FALSE` so R gives an error. – thelatemail May 14 '20 at 03:39
  • Ronak solved the problem I was facing. Thank you very much guys – Andre Masuko May 14 '20 at 03:44

2 Answers2

0

== is used for element-wise comparison, if you want to compare multiple values use %in% and turn the values to numeric/integer.

leaders$success <- as.integer(leaders$result %in% 
   c('dies within a day after the attack','dies between a day and a week',
   'dies between a week and a month','dies, timing unknown'))
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
0

We can also use + to coerce the logical to binary

leaders$success <- +(leaders$result %in% 
  c('dies within a day after the attack','dies between a day and a week',
   'dies between a week and a month','dies, timing unknown'))
akrun
  • 874,273
  • 37
  • 540
  • 662