1

Here is my code. I am trying to assign levels in a different column depending on what the numberPickOverall is. It is assigning levels, but the levels are completely wrong. I will attach an image so you can see what I mean. I have tried a hundred times but it just can not follow a simple if statement to assign the correct levels. Any advice would be appreciate or tips on what I am doing wrong.

for(i in 1:nrow(draft_raptor)){
  if(draft_raptor[i,24]==1){
    draft_raptor[i,27]<-"top pick"
    
  }
  else if(draft_raptor[i,24]>=2 &draft_raptor[i,24]<=30){
    draft_raptor[i,27]<-"top 3"
  }
  else if(draft_raptor[i,24]>=4 &draft_raptor[i,24]<=5){
    draft_raptor[i,27]<-"top 5"
  }
  else if(draft_raptor[i,24]>=6 &draft_raptor[i,24]<=10){
    draft_raptor[i,27]<-"top 10"
  }
  else if(draft_raptor[i,24]>=11 &draft_raptor[i,24]<=14){
    draft_raptor[i,27]<-"late lottery"
  }
  else if(draft_raptor[i,24]>=15 &draft_raptor[i,24]<=23){
    draft_raptor[i,27]<-"mid 1st round"
  }
  else if(draft_raptor[i,24]>=24 &draft_raptor[i,24]<=30){
    draft_raptor[i,27]<-"late first"
  }
  else if(draft_raptor[i,24]>=31 &draft_raptor[i,24]<=40){
    draft_raptor[i,27]<-"early 2nd"
  }
  else if(draft_raptor[i,24]>=41 &draft_raptor[i,24]<=50){
    draft_raptor[i,27]<-"mid 2nd"
  }
  else if(draft_raptor[i,24]>=51 &draft_raptor[i,24]<=60){
    draft_raptor[i,27]<-"late 2nd"
  }
}

Below are some of the results, as you can see, it is totally misplacing the levels and I truly have no idea why. Any help would be appreciated.


 
 
draft_level
<chr>
numberPickOverall
<int>
1   early 2nd   32      
2   early 2nd   32      
3   early 2nd   32      
4   top 3   26      
5   top 3   26      
6   top 3   26      
7   top 3   26      
8   top 3   26      
9   top 3   26      
10  top 3   26  
11  top 3   26      
12  top 3   26      
13  top 3   26      
14  top 3   26      
15  top 3   4       
16  top 3   4       
17  top 3   4       
18  top 3   4       
19  mid 2nd 49      
20  mid 2nd 49  
Luke Lilly
  • 11
  • 1
  • Oonce you evaluatate the `draft_raptor[i,24]>=2 &draft_raptor[i,24]<=30` the next else if is not executed `draft_raptor[i,24]>=4 &draft_raptor[i,24]<=5` because this condition is already met in the previous one. You may need to change the order of those statements – akrun Aug 21 '21 at 23:13
  • It is not clear with that 2 to 30 case and 4 and 5 because second one is clearly overlapping with the first case. similarly the other conditions as well with the first one. Probably, you take that condition out and then execute. Also, it is not clear which value should be in the final case when column value is 4. – akrun Aug 21 '21 at 23:18
  • Based on the logic, i think you have a typo i.e. `<=30` should be `<=3` – akrun Aug 21 '21 at 23:19
  • Try solutions from this post https://stackoverflow.com/questions/12979456/categorize-numeric-variable-into-group-bins-breaks – Ronak Shah Aug 22 '21 at 01:19

1 Answers1

1

There seems to be overlapping condition

draft_raptor[i,24]>=2 &draft_raptor[i,24]<=30

with the other else if conditions that may not get executed i.e.

draft_raptor[i,24]>=4 &draft_raptor[i,24]<=5

once the first condition is met.


Based on the value assigned, 30 could be 3

for(i in 1:nrow(draft_raptor)){
  if(draft_raptor[i,24]==1){
    draft_raptor[i,27]<-"top pick"
    
  }
  else if(draft_raptor[i,24]>=2 &draft_raptor[i,24]<=3){ ## changed here
    draft_raptor[i,27]<-"top 3"
  }
  else if(draft_raptor[i,24]>=4 &draft_raptor[i,24]<=5){
    draft_raptor[i,27]<-"top 5"
  }
  else if(draft_raptor[i,24]>=6 &draft_raptor[i,24]<=10){
    draft_raptor[i,27]<-"top 10"
  }
  else if(draft_raptor[i,24]>=11 &draft_raptor[i,24]<=14){
    draft_raptor[i,27]<-"late lottery"
  }
  else if(draft_raptor[i,24]>=15 &draft_raptor[i,24]<=23){
    draft_raptor[i,27]<-"mid 1st round"
  }
  else if(draft_raptor[i,24]>=24 &draft_raptor[i,24]<=30){
    draft_raptor[i,27]<-"late first"
  }
  else if(draft_raptor[i,24]>=31 &draft_raptor[i,24]<=40){
    draft_raptor[i,27]<-"early 2nd"
  }
  else if(draft_raptor[i,24]>=41 &draft_raptor[i,24]<=50){
    draft_raptor[i,27]<-"mid 2nd"
  }
  else if(draft_raptor[i,24]>=51 &draft_raptor[i,24]<=60){
    draft_raptor[i,27]<-"late 2nd"
  }
}

Or instead of doing this in a loop, a more easier vectorized approach is with case_when

library(dplyr)
v1 <- draft_raptor[[24]]
case_when(v1 == 1~ "top pick",
           v1 >= 2 & v1 <=3 ~ "top 3",
           v1 >= 4 & v1 <=5 ~ "top 5",
           v1 >= 6 & v1 <= 10 ~ "top 10",
           v1 >= 11 & v1 <= 14 ~ "late lottery",
           v1 >= 15 & v1 <= 23 ~ "mid 1st round",
           v1 >= 24 & v1 <= 30 ~ "late first",
           v1 >= 31  & v1 <= 40 ~ "early 2nd",
           v1 >= 41 & v1 <= 50 ~ "mid 2nd",
           v1 >= 51 & v1 <= 60 ~ "late 2nd")

Or may also use cut or findInterval with the specified breaks

akrun
  • 874,273
  • 37
  • 540
  • 662