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]]
On Wed, Aug 4, 2010 at 8:33 PM, Steven Kang <stochastickang at gmail.com> wrote:> 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. >The "zoo-quickref" vignette in the zoo package defines a one line nextfri function which does this.
Hello, I have a dataframe with data such as:> dat$BEGINDATUM[3][1] 13-09-2007> dat$BEGINDATUM[4][1] 01-11-2007> class(dat$BEGINDATUM[3])[1] "factor" Now I need to make calculation with these dates. But I get these result:> as.date(as.character(dat$BEGINDATUM[3]))[1] <NA>> as.date(as.character(dat$BEGINDATUM[4]))[1] 11Jan2007 How can i convert these factors to make calculations possible. Thanks for the answer, Andre [[alternative HTML version deleted]]
Hi, I think you just need to add the format = argument. Does this help?> x <- factor("01-11-2007") > as.character(x)[1] "01-11-2007"> as.Date(as.character(x), format = "%d-%m-%Y")[1] "2007-11-01" Cheers, Josh On Fri, Sep 3, 2010 at 2:11 PM, Andr? de Boer <rnieuws at gmail.com> wrote:> > Hello, > > I have a dataframe with data such as: > > dat$BEGINDATUM[3] > [1] 13-09-2007 > > dat$BEGINDATUM[4] > [1] 01-11-2007 > > > class(dat$BEGINDATUM[3]) > [1] "factor" > > Now I need to make calculation with these dates. > But I get these result: > > as.date(as.character(dat$BEGINDATUM[3])) > [1] <NA> > > as.date(as.character(dat$BEGINDATUM[4])) > [1] 11Jan2007 > > How can i convert these factors to make calculations possible. > > Thanks for the answer, > Andre > > ? ? ? ?[[alternative HTML version deleted]] > > ______________________________________________ > R-help at 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.-- Joshua Wiley Ph.D. Student, Health Psychology University of California, Los Angeles http://www.joshuawiley.com/