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

  • SEARCH
  • Home
  • 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 916777
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T18:06:10+00:00 2026-05-15T18:06:10+00:00

I am trying to convert a uncommon date format into a standard date. Basically

  • 0

I am trying to convert a uncommon date format into a standard date. Basically I have a dataset that contains a period with semiannual frequency formatted like: 206 denoting the second half of 2006, 106 denoting the first half and so forth. In order to rearrange it to 2006-06-01 respectively 2006-01-01, i have written a small function:

period2date = function(period)
{
check=list()
check=strsplit(as.character(period),split="")
x=as.numeric(check[[1]][1])
p=ifelse( x >= 2,6,1)
x=2

out=paste(x,"0",check[[1]][2],check[[1]][3],"-",p,"-1",sep="")
out=as.Date(out)


return(out)    
}

you may laugh now 🙂 . Anyway, that function works and here comes the problem. I want to apply this function to the time column of data.frame. I tried the following:

as.data.frame(lapply(mydf$period,period2date)) 

which returned the result closest to what I want:
structure.13665..class….Date..
1 2006-06-01

and so forth.. obviously i´d love to keep the name of my column – or even better just add the newly formatted date to my original df. Plus I tried:

sapply(mydf$period,period2date) # with results equal to the line below
unlist(lapply(mydf$period,period2date))

[1] 13300 13514 13665

All I want to do is change the uncommon 206 etc. format to 2006-06-01 (which works) and add a column to mydf (which does not work)

thx for any suggestions in advance!

  • 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-05-15T18:06:11+00:00Added an answer on May 15, 2026 at 6:06 pm

    R stores dates as numbers, so I think you’re getting some wacky behavior because you’re operating on the date output (i.e., putting the dates back into a matrix, which makes them appear as the numbers they really are). Instead, you should explicitly use a data.frame with data.frame(). Also, you may save some time if you use vectorized operations (I think the apply family still uses loops):

    period2date <- function(period) {
        period <- as.character(period)
        half <- substr(period, 1, 1)
        year <- substr(period, 2, 3)
        dates <- as.Date(ifelse(half=="1", paste(year, "0101", sep=""), paste(year, "0701", sep="")), format="%y%m%d")
        return(dates)
    }
    
    data <- data.frame(data, period2date(data$dates))
    

    You can make this cleaner by replacing vice appending the period/date column, also.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

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.