简体   繁体   中英

Trying to run an IF statement inside a FOR loop in R

I am trying to create a new column in my dataset and populate it with text based on a value in another column. The data does not only containt ST. There are other positions but just this is the one I am testing with.

I have being trying to do this by using an IF statement inside an FOR loop

Current Data (There is not only one person, this is just the player I am testing with)

short_name    team_position    position_group
J. Obika          ST                NA
D. Payet          LW                NA
D. Luiz           CB                NA

Below is how I want the data to look

short_name    team_position    position_group
J. Obika          ST              FORWARD
D. Payet          LW              FORWARD
D. Luiz           CB              DEFENDER

Below is some code I tried

for(i in 1:length(fifa20datasample$team_position)){
  if(fifa20datasample$team_position[i] == 'ST'){
    fifa20datasample$position_group[i] == "FORWARD"
  }
}

Error in if (fifa20datasample$team_position[i] == "ST") {: missing value where TRUE/FALSE needed

for(i in 1:length(fifa20datasample$team_position)){
      if(fifa20datasample$team_position[i] == "ST"){
        fifa20datasample$position_group[i] == "FORWARD"
      }
}

Error in if (fifa20datasample$team_position[i] == "ST") {: missing value where TRUE/FALSE needed

for(i in 1:length(fifa20datasample$team_position)){
  if(class(fifa20datasample$team_position[i]) == 'ST'){
    fifa20datasample$position_group[i] == "FORWARD"
  }
}

This does not do anything and does not print an error

for(i in 1:length(fifa20datasample$team_position)){
  if(class(fifa20datasample$team_position[i]) == "ST"){
    fifa20datasample$position_group[i] == "FORWARD"
  }
}

This does not do anything and does not print an error

Edit: I also tried the below for each of the above options

for(i in 1:length(fifa20datasample$team_position)){  
     if(fifa20datasample$team_position[i] == 'ST'){
     fifa20datasample$position_group[i] <- "FORWARD"   
      } 
 }

case_when from the dplyr library would be really helpful in this case. It abstracts away alot of the iteration issues that you're seeing.

The sample you've given only has one player and one position, I've included some more options in my example so that you can see how you can really easily cover every other position.

case_when works by going through the data frame and letting you check a condition against a column and amending a column in the same row based on that condition.

https://dplyr.tidyverse.org/reference/case_when.html

library(tibble)
library(dplyr)

fifa20datasample <- tibble(
        short_name = 'J. Obika', 
        team_position = 'ST', 
        position_group = NA
)


fifa20datasample %>% 
        mutate(
                position_group = case_when(
                        team_position %in% c('ST', 'LW') ~  'FORWARD', 
                        team_position %in% c('CAM', 'CM') ~  'MIDFIELD', 
                        team_position %in% c('CB') ~  'DEFENDER', 
                        team_position %in% c('GK') ~  'GOALKEEPER'
                )
        )


  short_name team_position position_group
  <chr>      <chr>         <chr>         
1 J. Obika   ST            FORWARD  

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM