Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8247657
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T22:55:21+00:00 2026-06-07T22:55:21+00:00

I have a basic character date to POSIXct function charDateToPosixct <- function(chrDate, format, timeZone)

  • 0

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".

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T22:55:22+00:00Added an answer on June 7, 2026 at 10:55 pm

    Use an anonymous function in apply like this

    apply(df, 1, function(x) charDateToPosixct(x[1], "%m/%d/%y", x[2]))
    #[1] 1587787200 1587704400 1587020400 1593489600
    

    edit

    You could convert the numeric vector you got using apply to POSIXct

    as.POSIXct(apply(df, 1, function(x) charDateToPosixct(x[1], "%m/%d/%y", x[2])), origin='1970-01-01')
    [1] "2020-04-25 05:00:00 CDT" "2020-04-24 06:00:00 CDT" "2020-04-16 08:00:00 CDT" "2020-06-30 05:00:00 CDT"
    

    Or, you can use lapply to get a list of POSIXct objects. Then you can combine with do.call c, or Reduce (You have to use as.character because you implicitly used stringsAsFactors=TRUE when you created the data.frame)

    do.call(c, lapply(seq_len(NROW(df)), function(i) {
      charDateToPosixct(as.character(df$date[i]), "%m/%d/%Y", as.character(df$timezone[i]))
    }))
    

    Or, if you have 2 vectors, you don’t even need the data.frame

    do.call(c, lapply(seq_along(chrDate), function(i) {
        charDateToPosixct(chrDate[i], "%m/%d/%Y", timeZone[i])
    }))
    

    Or, using Reduce

    Reduce(c, lapply(seq_along(chrDate), function(i) {
        charDateToPosixct(chrDate[i], "%m/%d/%Y", timeZone[i])
    }))
    

    My timezone is CDT, so any of the above give me

    [1] "2012-04-24 23:00:00 CDT" "2012-04-24 00:00:00 CDT" "2012-04-16 02:00:00 CDT" "2012-06-29 23:00:00 CDT"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We have a web app that stores and works with data with character encoding
I have a program that generates a single random character, using the randomCharacter function,
I have a basic AJAX form submission function in the head of my page
They're both resizable arrays, and std::basic_string doesn't have any specifically character-related functions like upper().
I have basic Spring MVC 3 setup for i18n where I can show labels
I have basic idea on Kilo Virtual Machine on Mobiles , I have clear
I have basic hello word example of Prime Faces. I have created dynamic web
I have a basic nServicebus 3.2.0 setup with a web application endpoint and a
I have a basic web application written in Java, running on a tomcat on
I have a basic PHP form (a few fields and 2 checkboxes, I want

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.