mercredi 13 juin 2018

If/else function on a data frame to create conditional matrix

Here is the data set for reproducibility:

    a=c(90.41,37.37,18.98)
    b=c(103.39,39.44,51.68)
    c=c(83.51,36.41,47.46)
    d=c(94.60,38.57,50.22)
    e=c(95.04,38.81,50.49)
    xx=t(data.frame(a,b,c,d,e))
    df=data.frame(xx)

And here is the if/else function I am trying run on the data frame

 classify=function(df){
 if (df>=110) {
 class="5"}
 else if (df<110 & df>=103){
 class="4"}
 else if (df<103 & df>=95){
 class="3"}
 else if (df<95 & df>=76){
 class="2"}
 else if (df<76){
 class="1"}
 else {class="none"}
 }  

However, what I want the if/else function to produce is a new data frame that looks like this:

     df
     X1 X2 X3
    a  2  1  1
    b  4  1  1
    c  2  1  1
    d  2  1  1
    e  3  1  1

I am unsure as to how to do this so any help would be super appreciated. I anticipate something is wrong in the if/else function itself but I am quite inexperienced and I don't know how to detect errors in the script that easily. Thank you!

Aucun commentaire:

Enregistrer un commentaire