在R中跳过for循环中的空数据框 [英] Skipping empty data frame in for loop in R
问题描述
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屋!