0

this is the only code i can think of

matik=c(70, 25, 78, 90, 67, 77, 83, 99, 45, 76, 45, 24, 55, 70, 65, 75, 68,89,55,68)
AB=c(71:80)
B=c(66:70)
BC=c(61:65)
C=c(56:60)
D=c(41:55)

matik2<-matik
if(matik>=81){
  print("A")
}else if(matik==AB){
  print("AB")
}else if(matik==B){
  print("B")
}else if(matik==BC){
  print("BC")
}else if(matik==C){
  print("C")
}else if(matik==D){
  print("D")
}else{
  print("E")
}

and when i run the code the result would be like this

[1] "E"
Warning messages:
1: In if (matik >= 81) { :
  the condition has length > 1 and only the first element will be used
2: In if (matik == AB) { :
  the condition has length > 1 and only the first element will be used
3: In if (matik == B) { :
  the condition has length > 1 and only the first element will be used
4: In if (matik == BC) { :
  the condition has length > 1 and only the first element will be used
5: In if (matik == C) { :
  the condition has length > 1 and only the first element will be used
6: In matik == D :
  longer object length is not a multiple of shorter object length
7: In if (matik == D) { :
 

ps. i am still new to R programming so i dont really many code. can you help me to find solution to this problem

  • 1
    You can't use a vector with an `if` statement. A better option might be a `dplyr::case_when` or in this case you are really splitting a group into ranges so the a base `cut()` function should work well. – MrFlick Sep 04 '21 at 05:10
  • See existing answers at: https://stackoverflow.com/questions/27415071/convert-percentage-to-letter-grade-in-r or https://stackoverflow.com/questions/40002209/trying-to-convert-a-column-of-number-grades-to-letter-grades-within-a-function – MrFlick Sep 04 '21 at 05:12
  • 1
    Use `%in%` instead of `==`. Also you should use `ifelse` instead of `if`/`else`. – Ronak Shah Sep 04 '21 at 05:27

0 Answers0