First of all, I know that there are many questions on SO about if/else statements in R, but none of them has been helpful for my specific situation and I've been struggling with this for a while.
I have a dataframe that looks like this:
metricx <- c(5, 4.8, 4.4, 3.6, 3.2, 2.1, 1.9, .5, .3, .1)
df <- as.data.frame(metricx)
I need to create two new variables based on the value of metricx (risk and answer).
I know this works....
df$risk <- ifelse(df$metricx >= 4.5, 'VERY HIGH', 'HIGH')
df$risk <- ifelse(df$metricx < 3.5, 'MEDIUM', df$risk)
df$risk <- ifelse(df$metricx < 2, 'LOW', df$risk)
But obviously not an elegant or efficient way to do it, since I would have to do this several times (my dataset is very large and i have more groups than this). My understanding is that R has to run through every record each time ifelse is called, so a chained option would be better.
I have tried this...
ifelse(df$metricx >= 4.5,
(df$risk <- 'VERY HIGH' &
df$answer <- 'Y')
,
ifelse(df$metricx >= 3.5,
(df$risk = 'HIGH' &
df$answer = 'Y')
,
ifelse(df$metricx >= 2,
(df$risk = 'MEDIUM' &
df$answer = 'Y')
,
ifelse(df$metricx >= .40,
(df$risk = 'LOW' &
df$answer = 'Y')
,
(df$risk = 'LOW' &
df$answer = 'N')
)
)
)
)
And I have tried this...
if (df$metricx >= 4.5){
df$risk = 'VERY HIGH'
df$answer = 'Y'
} else if (df$metricx >= 3.5){
df$risk = 'HIGH'
df$answer = 'Y'
} else if (df$metricx >= 2){
df$risk = 'MEDIUM'
df$answer = 'Y'
} else if (df$metricx >= .40){
df$risk = 'LOW'
df$answer = 'Y'
} else {
df$risk = 'LOW'
df$answer = 'N'
}
and they both give different errors, neither of which I can understand. I have looke at several different sites attempting to explain, but still cannot figure out how to do this.
My questions: 1. Why are my solutions not working? They appear to follow the syntax I have seen on the R site? 2. What is the correct way to achieve my desired output?
risk <- c('VERY HIGH', 'VERY HIGH', 'HIGH', 'HIGH', 'MEDIUM', 'MEDIUM', 'LOW', 'LOW', 'LOW', 'LOW')
answer <- c('Y','Y','Y','Y','Y','Y','Y','Y','Y', 'N')
want <- data.frame(metricx, risk, answer)