I bought a book so I could practice R, and this is a question I'm having a hard time with. The data concerns assassinations on leaders.
I first create an array object to represent the levels of leaders$result.
levels <- levels(leaders$result)
When calling levels, this is the output:
[1] "dies between a day and a week"
[2] "dies between a week and a month"
[3] "dies within a day after the attack"
[4] "dies, timing unknown"
[5] "hospitalization but no permanent disability"
[6] "not wounded"
[7] "plot stopped"
[8] "survives but wounded severely"
[9] "survives, whether wounded unknown"
[10] "wounded lightly"
Then I am asked to use indexing of the array object, 'levels', to create a binary variable that is equal to 1 if a leader dies, and 0 if they do not. I'm encouraged to use an if else statement.
success <- ifelse(levels==levels[c(1:4)],1,0)
The code above wont work for me. I want to be able to give any death results either a 1 or 0, using indexing. Does anyone have any tips?
After this, I am asked to store the new variable as part of the original data frame, so if I was to access this, I would be able to calculate successful attempts versus non-successful attempts (in results). How would I do this?