I have a basic character date to POSIXct function
charDateToPosixct <- function(chrDate, format, timeZone) {
as.POSIXct(chrDate, format=format, tz=timeZone)
}
I have a data frame with date as character and timezone.
chrDate <- c("4/25/2012","4/24/2012","4/16/2012","6/30/2012")
timeZone <- c("US/Eastern","US/Central","US/Pacific","US/Eastern")
df <- data.frame(date=chrDate,timezone=timeZone)
str(df)
'data.frame': 4 obs. of 2 variables:
$ date : Factor w/ 4 levels "4/16/2012","4/24/2012",..: 3 2 1 4
$ timezone: Factor w/ 3 levels "US/Central","US/Eastern",..: 2 1 3 2
I want to change the data type of date to POSIXct and so want to apply this function charDateToPosixct to each row with the format "%m/%d/%y".
Use an anonymous function in
applylike thisedit
You could convert the numeric vector you got using
applytoPOSIXctOr, you can use
lapplyto get a list ofPOSIXctobjects. Then you can combine withdo.callc, orReduce(You have to useas.characterbecause you implicitly usedstringsAsFactors=TRUEwhen you created thedata.frame)Or, if you have 2 vectors, you don’t even need the
data.frameOr, using
ReduceMy timezone is CDT, so any of the above give me