将数据存储在r中的循环中的矩阵中

时间:2015-10-13 20:04:19

标签: r

无法在矩阵中存储“移动”。它只存储运动“不动”的选项。这是代码的一小部分。任何帮助赞赏。感谢。

这是整个代码。对此感到抱歉,但这可能有所帮助。数据是进程是原始GPS数据,例如 记录编号日期时间Lat Long Speed Altitude

嗨Gregor,谢谢你的评论。我现在已经添加了整个代码,抱歉,我错过了所有源代码的顶部部分。不幸的是,它依赖于少数。历史长度存在问题,所以感谢你。但是,我还不能将输出保存到矩阵中吗?代码(按原样)仍然运行正常。

library(pracma)
library(zoo)


# Path fo file of raw GPS output
R<-read.csv("F://Cow1//R.csv") 

# path to distance function
source ("F://R functions//Distfunc.R")

# path to absolute heading function
source ("F://R functions//AbsHeading.R")

# path to Movement type function
source ("F://R functions//MovementType.R")

# path to bayes filter function
source ("F://R functions//bayesFilter.R")


# attach data
attach (R)






#Threshold for altitude
altThreshold <- 0.60 #0.6m threshold?

#Threshold angle for determining movement
angleThreshold <-40 #40 degrees

#Minimum speed for determining movement #0.3 m/s
minSpeed <-0.3

#Number of instances to consider as part of the Bayes inference
historyLength <-4

#Sampling period for GPS receiver
GPSSample <- 5 #5 second interval between samples 

#Fix accuracy of the GPS receiver
GPS_accy <-0.6 #0.6 metres

###############################################

########## Thresholds and Parameters Segmentation ###########

segmentSize <- 32 #32 movement objects per 'segment'

################################################

#Previous heading in order to work out track (left, right, forward, etc.)
previousHeading <- 0

#extract the instances, then the feature values for lat and long
#...then do calcs




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 <- matrix(data = movement, nrow = nrow(R), ncol = 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, 
  longtude_position_2 = long2, 
  Distance = gcd_vif,
  Speed = estSpeed,
  Acceleration = accel,
  Absolute_Heading = heading,
  Relative_Heading = relAngle,
  Movement = movement




)




}  
Results <- as.data.frame(do.call(rbind, lst)) # store the input data and the results in a data frame
Results

0 个答案:

没有答案