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.