I have one text file. I want to do some calculations (moving average) as shown below and write the results. The text file contains 15 columns and 601 rows.
columns <- paste0("X", 1:13)
ref <- read.table("D:\\DAS_asc.txt", sep="",header=TRUE)
library(zoo)
mean <- lapply(columns, function(column) {
rollapply(ref[[column]], 5, FUN = mean,na.rm=TRUE, fill=NA) })
when I just typed "mean" to see the results, I found that the calculations are not correct (I did it manually to check!). I thought it could be the movingave function itself but I checked and worked well:
s=c(1,5,2,4)
rollapply(s, 2, FUN = mean,na.rm=TRUE, fill=NA)
[1] 3.0 3.5 3.0 NA
rollapply(s, 3, FUN = mean,na.rm=TRUE, fill=NA)
[1] NA 2.666667 3.666667 NA
I wonder what is wrong when we do this with the text file?
first two lines of the file:
"0" "1" "2" "3" "4" "5" "6" "7" "8" "9" "10" "11" "12" "13" "14"
"2" 0.0493461075417879 0.0262911450465596 0.0426611743228151 0.0954854469641096 0.0933782886825547 0.218212200747129 0.285405481705908 0.226218243796976 0.146648210899044 0.115716572518044 0.0675501818197432 0.069120070466305 0.281314574594234 0.364434947521643 0.0124844383491671