I am new in R , and I am trying to do a script to calculate the sliding mean of some data.
This is how my data looks like:
Timestamp Accelerometer X
Accelerometer Y
Accelerometer Z
1 121219.757080078 -5.66180946541818 8.85684119781125 1.65407075345669
2 121239.288330078 -7.38255951126451 9.4117333531527 1.44410517346543
it has around 6000 rows, and I need to calculate the mean of Accelerometer x, Accelerometer Y, and Accelerometer Z each fifty rows. So from the data of row 1 to 50 I must get the mean of the 3 variables, then from rows 51 to 100, and so on until row 6000.
I tried with (for the first variable):
library(reshape2)
library(reshape)
x <- deadlift$`Accelerometer X`
win.size <- 50
slide <- 50
results <- data.frame(index=numeric(),win.mean=numeric())
i<-1
j<-1
while (i<length(x)) {
win.mean<-sum(x[i:(i+50)],na.rm = TRUE)/win.size
results[j,]<-c(i,win.mean)
i<-i+slide
j<-j+1
}
but I get this message:
Error in sum(x[i:(i + 50)], na.rm = TRUE) : invalid 'type' (character) of argument
any help?
Thank you.