[R] Replace for loop when vector calling itself

rivercode aquanyc at gmail.com
Mon Mar 7 06:34:01 CET 2011


Hi,

I am missing something obvious.  

Need to create vector as:

(0, i-1 + TheoP(i) - TheoP(i-1), repeat....) Where i is the index position
in the vector and i[1] is always 0.

Found myself having to use a For Loop because I could not get sapply
working.   Any suggestions ?
 
delta <- function(x) { 

	start = index[x]
	end = index[x+1] - 1 
	iTheo = start:end
	len = length(iTheo)
	theoP = as.numeric(TheoBA$Bid[iTheo])
	d = vector(mode = "numeric", length= len)
	d[1] = 0
	if (len>1) for (i in 2:len) { d[i] = d[i-1] + theoP[i] - theoP[i-1] }
	return(d)
}

Thanks,
Chris

--
View this message in context: http://r.789695.n4.nabble.com/Replace-for-loop-when-vector-calling-itself-tp3338383p3338383.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list