如何更改R中的日期格式

Pep*_*iCo -2 format r date

我有一些日期格式如下:

        V1  V2   V3
1 20100420 915   120
2 20100420 920   150
3 20100420 925   270
4 20100420 1530  281
Run Code Online (Sandbox Code Playgroud)

每行3列,第1行表示:2010-04-20 09:15 120

现在我想将其更改为1列(时间序列):

                   V3
1 20100420 09:15   120
2 20100420 09:20   150
3 20100420 09:25   270
4 20100420 15:30   281
Run Code Online (Sandbox Code Playgroud)

要么:

                   V3
1 20100420 9:15    120
2 20100420 9:20    150
3 20100420 9:25    270
4 20100420 15:30   281
Run Code Online (Sandbox Code Playgroud)

我怎么能在R中实现它?

the*_*ail 5

?strptime并且?sprintf是你的朋友:

重新创建数据集:

test <- read.table(textConnection("V1  V2 V3
20100420 915 120
20100420 920 150
20100420 925 270"),header=TRUE)
Run Code Online (Sandbox Code Playgroud)

做一些粘贴:

strptime(
paste(
    test$V1,
    sprintf("%04d", test$V2),
    sep=""
),
format="%Y%m%d%H%M"
)
Run Code Online (Sandbox Code Playgroud)

结果:

[1] "2010-04-20 09:15:00" "2010-04-20 09:20:00" "2010-04-20 09:25:00"
Run Code Online (Sandbox Code Playgroud)