This is probably less than an ideal solution, but it may be enough to get you started. The first part is based on a small modification of the find_peaks function from the gazetools package.
find_maxima <- function(x, threshold) { ranges <- find_peak_ranges(x, threshold) peaks <- NULL if (!is.null(ranges)) { for (i in 1:nrow(ranges)) { rnge <- ranges[i, 1]:ranges[i, 2] r <- x[rnge] peaks <- c(peaks, rnge[which(r == max(r))]) } } peaks } find_minima <- function(x, threshold) { ranges <- find_peak_ranges(x, threshold) peaks <- NULL if (!is.null(ranges)) { for (i in 1:nrow(ranges)) { rnge <- ranges[i, 1]:ranges[i, 2] r <- x[rnge] peaks <- c(peaks, rnge[which(r == min(r))]) } } peaks }
To find the find_maxima and find_minima functions, to give us what we are looking for, we will need to further smooth the total_gravity data:
spline <- smooth.spline(sample$loggingSample, y = sample$total_gravity, df = 30)
Note: "Zero" full gravity ( sample$total_gravity <- sample$total_gravity - 1 )
Then pull out the smoothed x and y values:
out <- as.data.frame(cbind(spline$x,spline$y))
Then we find local maxima and minima
max <- find_maxima(out$y, threshold = 0.4) min <- find_minima(out$y, threshold = -0.4)
And then build the data to make sure everything looks legal:
plot(out$y, type="l", col=grey(.2), xlab="Series", ylab="Gravity", main="Accelerometer Gravitational Force") lines(out$y, col="red") stdevs <- mean(out$y)+c(-2,-1,+1,+2)*sd(out$y) abline(h=stdevs) abline(v=max[1], col = 'green') abline(v=max[2], col = 'green') abline(v=min[1], col = 'blue')

And finally, we can see how long you have been on earth.
print(hangtime <- min[1] - max[1]) [1] 20
You can reduce your thresholds to get additional data (changes in acceleration).
Hope this helps!