Skipping empty data frame in for loop in R

天大地大妈咪最大 提交于 2019-12-11 07:35:01

问题


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){
...
}


来源:https://stackoverflow.com/questions/38348097/skipping-empty-data-frame-in-for-loop-in-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!