I mutate new column Grade
with calculate existing variables.If group not NA the take value Divided by Total,
Sample data:
df <- data.frame(value=c(62,71,69,70,66),class=letters[1:5],
group = c("A", "C", NA, "A", "B"),
Total = c(263, 249, 277, 221, 293))
I have tried this way,which obviously doesn't work.
df$Grade <- ifelse(is.na(df$group), NA, df$value[!is.na(df$group)]/Total)
Here is my desired output
value class group Total Grade
1 62 a A 263 0.235
2 71 b C 249 0.285
3 69 c <NA> 277 NA
4 70 d A 221 0.316
5 66 e B 293 0.225
Any tips is helpful.