Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fix date column in excel that imported to R as a mix of integers and character dates

Tags:

date

import

r

I am trying to import my excel data sheet and the date column is imported as a character column with some integer date values mixed in. enter image description here

Now the class of date column is character.

I am trying to adjust the date column using

Agri$Date <- as.Date(Agri$Date, format="%d/%m/%Y")

The result is the cells with numbers have NAs and the cells with the dates was adjusted right


2 Answers

The function convert_to_date() from the janitor package handles mixed date types like that.

For example, with your data:

Agri <- data.frame(Date = c("42864", "14/9/2017"))

Agri$Date <- janitor::convert_to_date(Agri$Date, character_fun = lubridate::dmy)

Agri
#>         Date
#> 1 2017-05-09
#> 2 2017-09-14
like image 78
mharinga Avatar answered Oct 17 '25 15:10

mharinga


library(lubridate)
Agri$Date_new<- dmy(Agri$Date)

Try by using above library.

like image 33
Nick C. Avatar answered Oct 17 '25 14:10

Nick C.



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!