[R] for loop not working
mnw
maw90 at aber.ac.uk
Fri Oct 9 17:08:56 CEST 2015
Hi. I have some code which loops through raw GPS data. This initial loop
calculates distance, angle, speed etc between successive coordinates as well
as type of movement e.g.left, right, forward. I want to construct a second
loop that goes through the movements and records 'Changes' as either '0' or
'1' depending on whether the movement changed or not. I have put the whole
code in for reference but the additional loop begins at the object 'holder.'
I want to store movements in holder and then loop through holder to
determine 'changes.' At the moment it gives me 'Error in holder[[t - 1]] :
attempt to select less than one element.' The moment i make holder [[t]] it
works but just gives a list of '0's. I have tried many different things and
just cannot get it to work, so any help would be very much appreciated.
Again, sorry for the long post.
lst <- list() # temporary list to store the results
for (i in seq(1, nrow(R) - 1)){ # loop through each row of the 'R' matrix
lat1 <- deg2rad(R[i, 4])
long1 <- deg2rad(R[i, 5])
lat2 <- deg2rad(R[i + 1, 4])
long2 <- deg2rad(R[i + 1, 5])
gcd_vif <- gcd.vif(lat1, long1, lat2, long2)
# calc estimated speed by mean of speeds between two GPS records
estSpeed <- (R[i, 6] + R[i+1, 6])/2
# calculate acceleration (velocity)
accel <- (R[i+1, 6] - R[i, 6]) / GPSSample
# calculate absolute heading
heading <- absoluteHeading(lat1, long1, lat2, long2)
# calculate bearing relative to previous GPS record
relAngle <- 0
# if the number of GPS records is less than 3 then no change in track
if (i < 1) relAngle = heading
# otherwise consider the previous heading in order to calculate the new
track
else if (i > 0) {
relAngle = (previousHeading - heading)
}
# determine whether movement is occurring and if so what type
# if there are insufficient history items then just record movement types
discretely
if (i < historyLength) movement <- movementType(relAngle, angleThreshold)
else if (i > historyLength-1) {
# Array to store speeds
speedHistory <- array(historyLength)
n = historyLength-1
# get the speeds from the previous n (hisoryLength) "Movements"
for (j in seq(1, length(historyLength))){
speedHistory [n] = R[i-j, 6]
n-1
}
if (!bayesFilter(speedHistory, minSpeed, GPS_accy)) movement <-
"non-moving"
else if(bayesFilter(speedHistory, minSpeed, GPS_accy)) movement <-
movementType(relAngle, angleThreshold)
}
holder <- list(movement)
holder [[i]] <- (movement)
for (t in length(holder)){
if (holder[[t]] == holder[[t-1]])
changes <- 0
else changes <- 1
}
# update previous heading information
previousHeading = heading
# Store the input data and the results
lst[[i]] <- c(
latitude_position_1 = lat1,
longtude_position_1 = long1,
latitude_position_2 = lat2,
longtude_position_2 = long2,
Distance = gcd_vif,
Speed = estSpeed,
Acceleration = accel,
Absolute_Heading = heading,
Relative_Heading = relAngle,
Movement = movement,
Changes = changes
)
}
Results <- as.data.frame(do.call(rbind, lst)) # store the input data and the
results in a data frame
Results
--
View this message in context: http://r.789695.n4.nabble.com/for-loop-not-working-tp4713392.html
Sent from the R help mailing list archive at Nabble.com.
More information about the R-help
mailing list