PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, October 8, 2022

[FIXED] How to make the format of dates same in the date column of a data frame?

 October 08, 2022     dataframe, date, r, statistics, time-series     No comments   

Issue

I have station wise Discharge data frame df. The dates (I imported it from an existing .csvfile) format are irregular. Below is an example data frame:

> df
  Station       Date Discharge
1       A 1981-01-01       0.1
2       A 1981-02-01       0.0
3       B 1981-03-01       0.0
4       B 1981-04-01       0.0
5       B 1/13/1981        0.4
6       C 1/14/1981        0.2
7       D 1/15/1981        0.6
8       D 1981-16-01       0.1
9       D 1981-17-01       0.5

Because of this further processing of this data is difficult. I tried the following:

> df$Date <- as.Date(df$Date, "%m/%d/%Y")
> df
  Station       Date Discharge
1       A 1981-01-01       0.1
2       A 1981-02-01       0.0
3       B 1981-03-01       0.0
4       B 1981-04-01       0.0
5       B NA               0.4
6       C NA               0.2
7       D NA               0.6
8       D 1981-16-01       0.1
9       D 1981-17-01       0.5

NA's are being introduced. How to make the format of all the dates same. It would be nice to have date as d-m-y format. Any guidance is appreciated. Thanks.


Solution

You can first use lubridate::parse_date_time to get data in standard format. Multiple formats can be passed in the function.

lubridate::parse_date_time(df$Date, c('Ydm', 'mdY'))

#[1] "1981-01-01 UTC" "1981-01-02 UTC" "1981-01-03 UTC" "1981-01-04 UTC" "1981-01-13 UTC"
#[6] "1981-01-14 UTC" "1981-01-15 UTC" "1981-01-16 UTC" "1981-01-17 UTC"

Then use format to get data in any format you wish.

format(lubridate::parse_date_time(df$Date, c('Ydm', 'mdY')), '%d-%m-%Y')

#[1] "01-01-1981" "02-01-1981" "03-01-1981" "04-01-1981" "13-01-1981" "14-01-1981"
#[7] "15-01-1981" "16-01-1981" "17-01-1981"

Note that the output from format is of class character and not date. Dates can have only one format in R which is Ymd.

as.Date(lubridate::parse_date_time(df$Date, c('Ydm', 'mdY')))

#[1] "1981-01-01" "1981-01-02" "1981-01-03" "1981-01-04" "1981-01-13" "1981-01-14"
#[7] "1981-01-15" "1981-01-16" "1981-01-17"


Answered By - Ronak Shah
Answer Checked By - David Goodson (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing