R语言 如何分离日期和时间

您所在的位置:网站首页 如何将日期分成三列 R语言 如何分离日期和时间

R语言 如何分离日期和时间

2024-07-13 14:20| 来源: 网络整理| 查看: 265

R语言 如何分离日期和时间

在这篇文章中,我们将在R编程语言中分离日期和时间。 日期-时间的格式是日期和时间(YYYY/MM/DD HH:MM:SS-年/月/日 Hours:Minute:Seconds)。

从时间戳中提取日期: 我们将通过使用as.Date()函数来提取日期。

语法:

as.Date(data)

其中数据是时间戳。

从时间戳中提取时间: 我们可以通过使用as.POSIXct()函数来做到这一点。要获得一个特定的时间小时格式,我们可以使用 format() 函数

语法:

format(as.POSIXct(data), format = “%H:%M”)

其中。

as.POSIXct()用于从时间戳中提取时间 format用于获取时间格式。例如:小时:分钟和秒 data是时间戳

例子1 :

# create variable with one time stamp  data ="2021/05/25 12:34:25"                               # extract date from the time stamp print( as.Date(data))    # get time from date using format in  # the form of hours and  minutes  print( format(as.POSIXct(data), format = "%H:%M"))

输出

如何在R语言中分离日期和时间?

例2 :

# create variable with one time stamp  data ="2021/05/25 12:34:25"                               # extract date from the time stamp print( as.Date(data))       # get time from date using format in the  # form of hours ,minutes  and seconds print( format(as.POSIXct(data), format = "%H:%M:%S"))

输出

如何在R语言中分离日期和时间?

例3 :

# create data  with five time stamps  data = c("2021/05/25 12:34:25",          "2019/1/14 04:10:30",          "2020/7/11 09:05:05",          "2018/1/14 04:10:30",          "2017/7/11 09:05:05")                               # extract date from the time stamp print( as.Date(data))    # get time from date using format in the # form of hours ,minutes  and seconds print( format(as.POSIXct(data), format = "%H:%M:%S"))

输出

如何在R语言中分离日期和时间?



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3