简体   繁体   English

用R转换日期格式

[英]Transform date format in 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, 如您所见,日期格式因用户使用iOS或Android而异,

My question, is there a way to transform the Android format date to d/m/y instead of dmy ? 我的问题是,有没有一种方法可以将Android格式的日期转换为d / m / y而不是dmy? Directly in the Date column or by creating a new one, 直接在“日期”列中或通过创建一个新列,

Thanks a lot 非常感谢

If you want to convert both types of strings directly to a date you could use the dmy() function in the lubridate package. 如果要将两种类型的字符串直接转换为日期,则可以使用lubridate包中的dmy()函数。

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"

您可以使用gsub将斜杠替换为破折号:

gsub("-", "/", mydata$Date)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM