I am adding a column to my data frame and with that column I am trying to group my data based on two conditions (difference in time and amount of distance). My code is composed of an ifelse statement for 50K observations, and it does just fine looping from row 1 to 7, but it refuses to loop past length 8. It didn't show me an error so I'm wondering, am I missing something in my code? Any help is greatly appreciated.
df
j <- 1
df$GroupID <- NA
df$GroupID[1] <- 1
for (i in 2:length(df)) {
flashes <- df[which(df$GroupID==j)]
h <- cbind(flashes$Long,flashes$Lat)
point <- cbind(df$Long[i],df$Lat[i])
lastrow <- tail(flashes, n = 1)
moment <- lastrow$DateTime
ifelse (min(spDistsN1(h,point,longlat = TRUE))<16 &
difftime(df$DateTime[i],moment)<minutes(15),
df$GroupID[i] <-j,df$GroupID[i] <-NA)
#arrange(df[, "GroupID"])
}
j=j+1
The first rows of my data looks like this:
DateTime Lat Long GroupID
2019-07-01 00:00:04 28.478 81.066 1
2019-07-01 00:00:04 28.479 81.068 1
2019-07-01 00:00:04 28.482 81.066 1
2019-07-01 00:00:04 28.475 81.085 1
2019-07-01 00:00:04 28.484 81.084 1
2019-07-01 00:00:04 28.492 81.080 1
2019-07-01 00:00:04 28.493 81.080 1
2019-07-01 00:00:04 28.493 81.081 1
2019-07-01 00:00:04 28.494 81.078 NA
2019-07-01 00:00:04 28.495 81.075 NA
2019-07-01 00:00:04 28.497 81.075 NA
2019-07-01 00:00:04 28.507 81.074 NA
Aucun commentaire:
Enregistrer un commentaire