Re: [R] for/if loop in R

2011-07-21 Thread financial engineer

thanks Rainer,

it worked!

> From: rainer.schuerm...@gmx.net
> To: r-help@r-project.org; fin_e...@hotmail.com
> Subject: Re: [R] for/if loop in R
> Date: Fri, 22 Jul 2011 03:23:19 +0200
> 
> For me, this works:
> 
> > Now, I want to add a 4th column, trend
> pricedata$trend <- 0
> 
> > which can have 2 values 0 or 1. if return>1%, trend=1 else trend=0.
> pricedata$trend <- ifelse( pricedata$return > .01, 1, 0 )
> 
> Rgds,
> Rainer
> 
> 
> On Thursday 21 July 2011 19:39:15 financial engineer wrote:
> > 
> > hi,
> > 
> > Can someone please help me figure out where I am making a mistake in my 
> > for/if loop:
> > 
> > I have a data frame (112 rows) called pricedata with 3 columns: date, 
> > prices, return.
> > 
> > Now, I want to add a 4th column, trend, which can have 2 values 0 or 1. if 
> > return>1%, trend=1 else trend=0.
> > 
> > so, this is what I did:
> > >trend<-numeric(nrow(pricedata))
> > >cbind(pricedata,trend)
> > >for(i in 2:nrow(pricedata)){
> > +if (return[i]>0.01) trend[i]=1 else trend[i]=0
> > +}
> > 
> > and it doesn't change the values in trend, despite the fact that the return 
> > column has several rows with values >0.01 -why?
> > 
> > thx!
> > 
> > 
> > 
> > 
> >   
> > [[alternative HTML version deleted]]
> > 
> > __
> > R-help@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.
  
[[alternative HTML version deleted]]

__
R-help@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.


Re: [R] for/if loop in R

2011-07-21 Thread Rainer Schuermann
For me, this works:

> Now, I want to add a 4th column, trend
pricedata$trend <- 0

> which can have 2 values 0 or 1. if return>1%, trend=1 else trend=0.
pricedata$trend <- ifelse( pricedata$return > .01, 1, 0 )

Rgds,
Rainer


On Thursday 21 July 2011 19:39:15 financial engineer wrote:
> 
> hi,
> 
> Can someone please help me figure out where I am making a mistake in my 
> for/if loop:
> 
> I have a data frame (112 rows) called pricedata with 3 columns: date, prices, 
> return.
> 
> Now, I want to add a 4th column, trend, which can have 2 values 0 or 1. if 
> return>1%, trend=1 else trend=0.
> 
> so, this is what I did:
> >trend<-numeric(nrow(pricedata))
> >cbind(pricedata,trend)
> >for(i in 2:nrow(pricedata)){
> +if (return[i]>0.01) trend[i]=1 else trend[i]=0
> +}
> 
> and it doesn't change the values in trend, despite the fact that the return 
> column has several rows with values >0.01 -why?
> 
> thx!
> 
> 
> 
> 
> 
>   [[alternative HTML version deleted]]
> 
> __
> R-help@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.

__
R-help@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.


[R] for/if loop in R

2011-07-21 Thread financial engineer

hi,

Can someone please help me figure out where I am making a mistake in my for/if 
loop:

I have a data frame (112 rows) called pricedata with 3 columns: date, prices, 
return.

Now, I want to add a 4th column, trend, which can have 2 values 0 or 1. if 
return>1%, trend=1 else trend=0.

so, this is what I did:
>trend<-numeric(nrow(pricedata))
>cbind(pricedata,trend)
>for(i in 2:nrow(pricedata)){
+if (return[i]>0.01) trend[i]=1 else trend[i]=0
+}

and it doesn't change the values in trend, despite the fact that the return 
column has several rows with values >0.01 -why?

thx!




  
[[alternative HTML version deleted]]

__
R-help@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.


Re: [R] for/if loop

2009-01-28 Thread SnowManPaddington

Hi ya, I've revised the code (and finally know what I m doing.. :-D)

The good news is.. I dont get any error message, but the bad news is the
following optim generate no results. I still think there is something to do
with my loop... can anyone advice? Thanks again!!!



pp=1
rr=1

for (ii in 1:n){
if (!(panel[ii] == pp)){
hll[pp,1] == sum(lselb1[rr:ii-1])
hll[pp,2] == sum(lselb2[rr:ii-1])
rr==ii
pp==pp+1
}

if (ii==n){
hll[pp,1] == sum(lselb1[rr:ii])
hll[pp,2] == sum(lselb2[rr:ii])
rr==ii
pp==pp+1
}
ii=ii+1
}





pp=1
rr=1

for (ii in 1:n){
if (!(panel[ii] == pp)){
hll[pp,1] == sum(lselb1[rr:ii-1])
hll[pp,2] == sum(lselb2[rr:ii-1])
rr==ii
pp==pp+1
}

if (ii==n){
hll[pp,1] == sum(lselb1[rr:ii])
hll[pp,2] == sum(lselb2[rr:ii])
rr==ii
pp==pp+1
}
ii=ii+1
}





SnowManPaddington wrote:
> 
> Hi, it's my first time to write a loop with R for my homework. This loop
> is part of the function. I wanna assign values for hll according to panel
> [ii,1]=pp. I didn't get any error message in this part. but then when I
> further calculate another stuff with hll, the function can't return. I
> think it must be some problem in my loop. Probably something stupid or
> easy. But I tried to look for previous posts in forum and read R language
> help. But none can help.. Thanks!
> 
> 
> 
> for (ii in 1:100){
>   for (pp in 1:pp+1){
>   for (rr in 1:rr+1){
>   if (panel[ii,1]!=pp)
>   {
>   hll(pp,1)=ColSums(lselb1(rr:ii-1,1))
>   hll(pp,2)=ColSums(lselb2(rr:ii-1,1)) 
>   rr=ii
>   pp=pp+1
>   }
>   else
>   {
>   hll(pp,1)=ColSums(lselb1(rr:ii,1))
>   hll(pp,2)=ColSums(lselb2(rr:ii,1)) 
>   rr=ii
>   pp=pp+1}
>   }
>   }}}
> 
> 
> in fact I have the corresponding Gauss code here. But I really don't know
> how to write such loop in R.
> 
> rr=1;
> ii=1;
> pp=1;
> do until ii==n+1;
>   if pan[ii,1] ne pp;
>   hll[pp,1]=sumc(lselb1[rr:ii-1,1]);
>   hll[pp,2]=sumc(lselb2[rr:ii-1,1]);
>   rr=ii;
>   pp=pp+1;
>   endif;
>   if ii==n;
>   hll[pp,1]=sumc(lselb1[rr:ii,1]);
>   hll[pp,2]=sumc(lselb2[rr:ii,1]);
>   rr=ii;
>   pp=pp+1;
>   endif;
>   ii=ii+1;
> endo;
> 
> 

-- 
View this message in context: 
http://www.nabble.com/for-if-loop-tp21701496p21715928.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@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.


Re: [R] for/if loop

2009-01-28 Thread jim holtman
Within the loops you are changing the loop variables (pp & rr).  Why
are you doing this?  THis might be causing your problem of what sounds
like an infinite loop.  You probably want to rethink what you are
trying to do in the loop.

On Wed, Jan 28, 2009 at 3:21 AM, SnowManPaddington  wrote:
>
> Hi, it's my first time to write a loop with R for my homework. This loop is
> part of the function. I wanna assign values for hll according to panel
> [ii,1]=pp. I didn't get any error message in this part. but then when I
> further calculate another stuff with hll, the function can't return. I think
> it must be some problem in my loop. Probably something stupid or easy. But I
> tried to look for previous posts in forum and read R language help. But none
> can help.. Thanks!
>
>
>
> for (ii in 1:100){
>for (pp in 1:pp+1){
>for (rr in 1:rr+1){
>if (panel[ii,1]!=pp)
>{
>hll(pp,1)=ColSums(lselb1(rr:ii-1,1))
>hll(pp,2)=ColSums(lselb2(rr:ii-1,1))
>rr=ii
>pp=pp+1
>}
>else
>{
>hll(pp,1)=ColSums(lselb1(rr:ii,1))
>hll(pp,2)=ColSums(lselb2(rr:ii,1))
>rr=ii
>pp=pp+1}
>}
>}}}
>
>
> in fact I have the corresponding Gauss code here. But I really don't know
> how to write such loop in R.
>
> rr=1;
> ii=1;
> pp=1;
> do until ii==n+1;
>if pan[ii,1] ne pp;
>hll[pp,1]=sumc(lselb1[rr:ii-1,1]);
>hll[pp,2]=sumc(lselb2[rr:ii-1,1]);
>rr=ii;
>pp=pp+1;
>endif;
>if ii==n;
>hll[pp,1]=sumc(lselb1[rr:ii,1]);
>hll[pp,2]=sumc(lselb2[rr:ii,1]);
>rr=ii;
>pp=pp+1;
>endif;
>ii=ii+1;
> endo;
>
> --
> View this message in context: 
> http://www.nabble.com/for-if-loop-tp21701496p21701496.html
> Sent from the R help mailing list archive at Nabble.com.
>
> __
> R-help@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.
>



-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem that you are trying to solve?

__
R-help@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.


Re: [R] for/if loop

2009-01-28 Thread Zhou Fang

What are you trying to do with
> for (pp in 1:pp+1){
?

Also, note that 1:rr+1 and 1:(rr+1) mean different things.

Zhou

__
R-help@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.


[R] for/if loop

2009-01-28 Thread SnowManPaddington

Hi, it's my first time to write a loop with R for my homework. This loop is
part of the function. I wanna assign values for hll according to panel
[ii,1]=pp. I didn't get any error message in this part. but then when I
further calculate another stuff with hll, the function can't return. I think
it must be some problem in my loop. Probably something stupid or easy. But I
tried to look for previous posts in forum and read R language help. But none
can help.. Thanks!



for (ii in 1:100){
for (pp in 1:pp+1){
for (rr in 1:rr+1){
if (panel[ii,1]!=pp)
{
hll(pp,1)=ColSums(lselb1(rr:ii-1,1))
hll(pp,2)=ColSums(lselb2(rr:ii-1,1)) 
rr=ii
pp=pp+1
}
else
{
hll(pp,1)=ColSums(lselb1(rr:ii,1))
hll(pp,2)=ColSums(lselb2(rr:ii,1)) 
rr=ii
pp=pp+1}
}
}}}


in fact I have the corresponding Gauss code here. But I really don't know
how to write such loop in R.

rr=1;
ii=1;
pp=1;
do until ii==n+1;
if pan[ii,1] ne pp;
hll[pp,1]=sumc(lselb1[rr:ii-1,1]);
hll[pp,2]=sumc(lselb2[rr:ii-1,1]);
rr=ii;
pp=pp+1;
endif;
if ii==n;
hll[pp,1]=sumc(lselb1[rr:ii,1]);
hll[pp,2]=sumc(lselb2[rr:ii,1]);
rr=ii;
pp=pp+1;
endif;
ii=ii+1;
endo;

-- 
View this message in context: 
http://www.nabble.com/for-if-loop-tp21701496p21701496.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@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.