0

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.

Sayantan4796
  • 169
  • 1
  • 10

1 Answers1

1

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"
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213