使用 R 将日期格式的字符串列表/向量转换为 posix 日期类 [英] Convert list/vector of strings with date format into posix date class with R

查看:21
本文介绍了使用 R 将日期格式的字符串列表/向量转换为 posix 日期类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个日期格式的字符串列表,我想将其转换为可以用 R 操作的 posix 日期列表,我该怎么做?

I have a list of strings that is in our date format and I want to convert it into a list of posix dates that I can manipulate with R, how can I do that?

这就是我所拥有的,但我最终得到了一个列表:

This is what I have but I end up with a list of lists:

 a <- c("2009.01.01 00:00:00", "2009.01.01 00:00:00")

z <- lapply(a,function(x){strptime(x, "%Y.%m.%d %H:%M:%S")})

> z <- lapply(a,function(x){strptime(x, "%Y.%m.%d %H:%M:%S")})
> summary(z)
     Length Class   Mode
[1,] 1      POSIXlt list
[2,] 1      POSIXlt list

推荐答案

strptime 是矢量化的:

a <- c("2009.01.01 12:20:10", "2009.01.01 04:12:14")
> out <- strptime(a, "%Y.%m.%d %H:%M:%S")
> str(out)
 POSIXlt[1:2], format: "2009-01-01 12:20:10" "2009-01-01 04:12:14"

这篇关于使用 R 将日期格式的字符串列表/向量转换为 posix 日期类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆