For loop problems in R -


i have 365x1 vector called dv (day value). applying function generate hourly values, hv. codes follows:

sigma<-0.246*dv stime<-function(x,y,z){(exp(-(x-12)^2/(2*y^2))+cos(pi*(x-12)/(z-1)))/(2*y*sqrt(2*pi))} t<-1:24 hv<-null (i in seq(along=t)){hv<-c(hv,mapply(stime,t[i],sigma,dv))} 

i want first 24 values using t[1],t[2],...t[24] z=dv[1] , next 24 values use z=dv[2] etc. code have first 365 values dv[1],dv[2]...dv[365] take t[1] next 365 values take t[2]...i not sure if explain clearly. appreciate help.

r vectorised exp, cos, sqrt , arithmetic operators need loop along dv values.

here example using function sapply, , first 3 t values brevity...

dv <- 1:5 sapply( dv , function(i) stime( x = t[1:3] , y = 0.246*i , z = ) )       [,1]       [,2]          [,3]       [,4]          [,5] [1,]  nan -0.4054291 -6.621773e-16  0.1013573 -1.146727e-01 [2,]  nan  0.4054291 -2.702861e-01 -0.1013573  7.689812e-16 [3,]  nan -0.4054291  1.489523e-16 -0.2027146  1.146727e-01 

so there 1 value z (which current dv value), 1 value y 0.246 * current dv value, , 3 values of t x. output 3 values, 1 each t, sigma , dv values recycled , reused each time.

the nans in first column caused fact in example first value of t , dv 1, leads

cos(pi * (1 - 12)/(1 - 1)) 

which simplifies to;

cos(-inf) nan 

so result must nan in example (hopefully not case in real data).


Comments

Popular posts from this blog

css - Which browser returns the correct result for getBoundingClientRect of an SVG element? -

gcc - Calling fftR4() in c from assembly -

Function that returns a formatted array in VBA -