This seems more like an R-Help question than an R-Finance Question, but
here goes:
For the year example, you could use the month.day.year from the chron
package (here I chose to add a column to the data frame that adds the day):

library(xts)
library(chron)
n <- 100
df <- data.frame(Date = as.POSIXct(as.Date(1:n)), Value = rnorm(n))
df$Day <- month.day.year(df$Date)$day

Also, if your condition is Boolean, you can use the vectorized ifelse
function:
df$Condition <- ifelse(df$Value > 0, 1, 0)

I don't know exactly what your "condition" is, so if you are more specific
we could help you more.  But say there are three alternatives, you could do
something like:

df$Condition2 <- "NotSet"
opts <- c("LessThan0", "Between0and1", "GreaterThan1")
for(i in 1:length(opts)){
    switch(i,{
        df[df$Value < 0, "Condition2"] <- opts[i]
    }, {
        df[df$Value > 0 & df$Value < 1, "Condition2"] <- opts[i]
    },{
        df[df$Value > 1, "Condition2"] <- opts[i]
    })
}

HTH
-Bob

On Wed, May 16, 2012 at 11:39 AM, Geoffrey Smith <gps@asu.edu> wrote:

> Hello, is there anyone who can show me how to write a function or loop that
> cycles through a data frame and creates a new variable based on some
> condition?  For example, suppose I want to create a variable called FYEAR
> in a data frame conditional on the variable DATE, as follows:
>
> DF$FYEAR[DF$DATE = as.Date('1992-06-30')] <- 1991;
> DF$FYEAR[DF$DATE = as.Date('1993-06-30')] <- 1992;
> DF$FYEAR[DF$DATE = as.Date('1994-06-30')] <- 1993;
> DF$FYEAR[DF$DATE = as.Date('1995-06-30')] <- 1994;
> DF$FYEAR[DF$DATE = as.Date('1996-06-30')] <- 1995;
>
> and so on...
>
> (PS this is just an example.  I know I can get the FYEAR straight from the
> DATE.)
>
> Is there any way to automate this using a for loop or something like that?
>  Thank you!!  Geoff
>
>        [[alternative HTML version deleted]]
>
> _______________________________________________
> R-SIG-Finance@r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-sig-finance
> -- Subscriber-posting only. If you want to post, subscribe first.
> -- Also note that this is not the r-help list where general R questions
> should go.
>

	[[alternative HTML version deleted]]

