Chapter 9 For loops and while loops.
Many times we want to loop though the data for perform opperations. For this we can use for loops.
for(player in hits_data_frame$PlayerID){
# Print player name with a little introduction
print(paste0("Player Name: " , player))
}
## [1] "Player Name: Player01"
## [1] "Player Name: Player02"
## [1] "Player Name: Player03"
## [1] "Player Name: PlayerNew"
## [1] "Player Name: Player05"
We can also do this using a while loop. A while loop continues until a certain contiditon is met.
## Create count variable that will be updated each time through
count = 1
## find amount of rows in the data frame.
row_count <- nrow(hits_data_frame)
while(count <= row_count){
# Print player name with a little introduction
print(paste0("Player Name: ", hits_data_frame$PlayerID[count]))
# Update count
count = count+1
}
## [1] "Player Name: Player01"
## [1] "Player Name: Player02"
## [1] "Player Name: Player03"
## [1] "Player Name: PlayerNew"
## [1] "Player Name: Player05"