Hi,
I have a series of data (y) against the time of day (x), for simplicity
consider:
x=c(0,1,2,3,4,5,6,7,8,9,10)
y=c(0,1,2,3,0,-1,-2,1,2,1,0)
I wish to loop through the y values and and at the point where they touch or
cross the x axis, subtract the previous times of day from this point, and
calculate the y weighted average time for this interval. The loop then needs to
continue to the next interval bounded by the points where y=0 and do the same.
I will finally take the arithmetic average all of these to come up with an
average value of time.
To clarify by considering the simplified values above, the program needs to
return:
(4*0+3*1+2*2+1*3)/(0+1+2+3) = 1.66
(3*0+2*1+1*2)/(0+1+2)=1.33
(2*2+1*1)/(2+1)=1.66
returning an average time of 1.55
the final value of the sequence is always 0.
considering just the first interval i could use something like
for(i in 1:length(x))print(abs(y[i])*x[length(x)]-x[i])
but how do I sum over these values and divide by the sum of the y values in
that interval? Secondly how do I continue iterating through the data over each
subsequent interval? I have found this fairly easy to code in Fortran but I
need to do it in R which is relatively new to me. Any suggestions on improving
the method would also be appreciated if it is over complicating things
Many thanks,
Ryan
[[alternative HTML version deleted]]
______________________________________________
[email protected] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.