Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Transform date format in R

Tags:

date

r

I am datamining the data from a mobile application, I have a simple problem that is reccurring that is giving me trouble,

Database

UserId         Platform         Date
1              Android          01-01-2016
2              iOS              02/01/2016
3              Android          03-01-2016
4              Android          04-01-2016

As you can see the format of the date is different depending whether the user is on using iOS or Android,

My question, is there a way to transform the Android format date to d/m/y instead of d-m-y ? Directly in the Date column or by creating a new one,

Thanks a lot

like image 235
Alban Couturier Avatar asked Aug 03 '26 12:08

Alban Couturier


2 Answers

If you want to convert both types of strings directly to a date you could use the dmy() function in the lubridate package.

library(lubridate)

date.vector <- c("01-01-2016", "02/01/2016", "03-01-2016", "04-01-2016")

dmy(date.vector)

# > dmy(date.vector)
# [1] "2016-01-01" "2016-01-02" "2016-01-03" "2016-01-04"
like image 51
majom Avatar answered Aug 06 '26 02:08

majom


you can use gsub to replace dashes by slashes:

gsub("-", "/", mydata$Date)
like image 25
lebatsnok Avatar answered Aug 06 '26 02:08

lebatsnok



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!