Hi all,

I am trying to convert all the dates (all days that are not Friday) in data
frame into dates to next Friday.

The following works but the result is returned as vector rather than the
original class.

It would be greatly apprecited if you could provide any solution to this
problem.

Many thanks in advance.


# Define arbitrary initial date value
ini <- as.Date("2010/1/1", "%Y/%m/%d")
# Generate arbitrary data frame consisting of date values
dat <- data.frame(y1 = seq(ini, ini + 5, 1), y2 = seq(ini + 365, ini + 365 +
5, 1), y3 = seq(ini + 365*2, ini + 365*2 + 5, 1))
# Convert date values to next Friday
y <- sapply(dat, function(x) ifelse(weekdays(x, abbreviate = TRUE) %in%
"Mon", x + 4,
                                         ifelse(weekdays(x, abbreviate =
TRUE) %in% "Tue", x + 3,
                                          ifelse(weekdays(x, abbreviate =
TRUE) %in% "Wed", x + 2,
                                           ifelse(weekdays(x, abbreviate =
TRUE) %in% "Thu", x + 1,
                                            ifelse(weekdays(x, abbreviate =
TRUE) %in% "Sat", x + 6,
                                             ifelse(weekdays(x, abbreviate =
TRUE) %in% "Mon", x + 7, x)))))))

# Convert interger into date class
class(y) <- "Date"



-- 
Steven

        [[alternative HTML version deleted]]

______________________________________________
[email protected] 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.

Reply via email to