在R中跳过for循环中的空数据框 [英] Skipping empty data frame in for loop in R

查看:369
本文介绍了在R中跳过for循环中的空数据框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在研究一个项目,在这个项目中,我正在通过一个大型的数据框架来介绍某些事件的信息。在这些事件中,我有兴趣计算一个球的平均速度。任何人都可以这样做,我正在使用一个for循环,它首先对数据框进行子集化处理,得到一个只包含某个事件信息的数据框。之后,它会计算该事件的平均球速,并确定哪个球队拥有球。现在我遇到了一个问题,即有时事件不在我的大数据框中,因此基于该事件进行子集化将返回一个空的数据框。在这一点上我for循环遇到错误,并停止。我希望使用if语句来查看事件号是否高于0会有所帮助,但它给了我相同的结果。下面我发布了我在for循环中使用的代码。

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

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]< - 距离/时间

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
}
}

我是谁king for是一种跳过有时出现的空白数据框的方法。例如,事件1到30的工作完全正常,但由于某种原因,然后31不存在,当子集时返回一个空的数据框。

帮助将不胜感激

解决方案

检查行数df

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


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

解决方案

Check the number of rows of df

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

这篇关于在R中跳过for循环中的空数据框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆