在R中转换日期格式 [英] Converting Date formats in R
本文介绍了在R中转换日期格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
dates <- as.Date(dli$Dates)
class(dates)
[1] "Date"
dates
[1] "2016-01-01" "2016-01-02" "2016-01-03" "2016-01-04" "2016-01-05" "2016-01-06"
[7] "2016-01-07" "2016-01-08" "2016-01-09" "2016-01-10" "2016-01-11" "2016-01-12"
[13] "2016-01-13" "2016-01-14" "2016-01-15" "2016-01-16" "2016-01-17" "2016-01-18"
[19] "2016-01-19" "2016-01-20" "2016-01-21" "2016-01-22" "2016-01-23" "2016-01-24"
[25] "2016-01-25" "2016-01-26" "2016-01-27" "2016-01-28" "2016-01-29" "2016-01-30"
[31] "2016-01-31" "2016-02-01" "2016-02-02" "2016-02-03" "2016-02-04" "2016-02-05"
[37] "2016-02-06" "2016-02-07" "2016-02-08" "2016-02-09" "2016-02-10" "2016-02-11"
这是我的日期格式,因此我需要将其转换为 2016-month-day
我得到NA值
This is my date format , so i need to convert it into "2016-month-day" I am getting NA values
dates <- as.Date(dli$Dates,"%d/%b/%Y")
class(dates)
[1] "Date"
dates
[1] NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
[31] NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
[61] NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
[91] NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
[121] NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
您能提供任何建议吗?
提前谢谢
can you give any suggestions Thanks in Advance
推荐答案
获取所需的格式,例如 2016-month -day ,一旦将字符串向量转换为 Date
类型,就可以使用 format
函数。
To obtain your required format i.e., 2016-month-day , you can use format
function once you have converted vector of strings to Date
type.
我希望下面的代码片段可以消除您的疑问。
I hope below code snippet clears your doubt.
> d = c("2016-02-08","2016-02-18","2015-02-08","2016-02-02")
> class(d)
[1] "character"
> d = as.Date(d)
> class(d)
[1] "Date"
> d = format(d,"%Y-%b-%d")
> d
[1] "2016-Feb-08" "2016-Feb-18" "2015-Feb-08" "2016-Feb-02"
Format函数将日期类型的对象转换为所需的格式。请参阅此链接,以获取有关日期类型格式的更多信息。
Format function converts the date type objects into the required format. Refer to this link for more information on date type formatting.
这篇关于在R中转换日期格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文