[R] Replace for loop when vector calling itself

From: rivercode <aquanyc_at_gmail.com>
Date: Sun, 06 Mar 2011 21:34:01 -0800 (PST)


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.

______________________________________________
R-help_at_r-project.org 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.
Received on Mon 07 Mar 2011 - 08:55:39 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Mon 07 Mar 2011 - 13:50:19 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive