Skipping empty data frame in for loop in R

2019-07-24 22:44发布

问题:

I am currently working on a project in which I am going through a large data frame with information about certain events. In these events I am interested in calculating the average speed of a ball. Anywho to do this I am using a for loop which first subsets the data frame to get a data frame which only includes information from a certain event. Afterwards it calculates the average ball speed for that event and determines which team had possession of the ball. Now I have come upon a problem that sometimes an event is not in my large data frame, and therefore subsetting it based on that event returns an empty data frame. At this point my for loop runs into an error and stops. I hoped using an if statement to see if the event number is higher than 0 would help, but it gives me the same result. Below I posted the code I am using in my for loop.

    for(i in 1:484){
      df <- subset[which(subset$event.id == event), ]

      if(df$event.id >= 0){
        df <- df[rev(order(df$game_clock)),]

        distance <- travelDist(df$x_loc, df$y_loc)
        start.time <- max(df$game_clock)
        end.time <- min(df$game_clock)
        time <- start.time - end.time
        data.frame$speed.event[i] <- distance/time

        df$HOMEDESCRIPTION <- as.numeric(df$HOMEDESCRIPTION)
        df$VISITORDESCRIPTION <- as.numeric(df$VISITORDESCRIPTION)
        df[is.na(df)] <- 0

        if(df$HOMEDESCRIPTION[1] == 0){
           data.frame$team[i] <- "away"
           }else{
           data.frame$team[i] <- "home"
           }
     event <- event + 1

     }else{
      event <- event + 1 
     }
}

What I am looking for is a way to skip the empty data frame that sometimes occurs. For example events 1 through 30 work perfectly fine, but then 31 for some reason does not exist and when subsetting it returns an empty data frame.

Help would be much appreciated

回答1:

Check the number of rows of df

if (nrow(df)>0){
...
}