[R] how to deal with vector[0]?

Sharpie chuck at sharpsteen.net
Thu Mar 25 16:48:40 CET 2010



Jin wrote:
> 
> Hi,
> 
> I have  a vector with 4 elements, e.g., tau_i=c(100,200,300,400), but
> potentially tau_i[0]=0. In a "for" loop,
> 
> tau_i=c(100,200,300,400)
> m=4
> tau_i[0]=0  # <------- ?
> P_i=1
> 	for(i in 2:m)
> 	{
> 		P_i = P_i*(tau_i[i-1]-tau_i[i-2])
> 	}
> 
> 
> Error in P_i = P_i * (tau_i[k - 1] - tau_i[k - 2]): 
>   replacement has length zero
> 
> 
> Unfortunately, I can add this potential element into "tau_i" because it
> has been defined initially and used mainly throughout all procedures.
> 
> When I calculate 
>> k=2
>> tau_i[k-1]-tau_i[k-2]
> umeric(0)
> 
> But, it should be "100". Is there anyone can help me solve this problem?
> 
> 
> Best,
> 
> Jin
> NREL, Golden CO
> (303) 275-4642
> 

I'm a little confused on exactly what you're trying to achieve here-- vector
indexes in R start at 1, not 0, and so trying to assign any value to tau_i[
0 ] is meaningless.

Using tau_i[ 0 ] in a calculation is equivalent to using numeric( 0 ) --
i.e. a numeric vector of *length* zero, which contains no values at all.

Does this help?

-Charlie

-----
Charlie Sharpsteen
Undergraduate-- Environmental Resources Engineering
Humboldt State University
-- 
View this message in context: http://n4.nabble.com/how-to-deal-with-vector-0-tp1690812p1690861.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list