拆分日期时间 [英] Split date time

查看:133
本文介绍了拆分日期时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为开始的列datetime(DD:MM:yyyy HH:mm:ss)的df,我想将此列分成两个名为date和time的列。

现在我尝试了以下内容:

  df $ Date<  -  sapply(strsplit字符(df $ Start),),[,1)
df $ Time< - sapply(strsplit(as.character(df $ Start),),[,2)

但是,如果我使用函数str(df)可以看到我关注的变量)。


'data.frame':18363 obs。 19个变量:

$开始:因素w / 67级别2013-09-01 08:07:41.000,..:1 1 1 1 1 1 1 1 1 1 ...

$间隔:int 47259 47259 47259 47259 47259 47259 47259 47259 47259 47259 ...

$ DateTime:因素w / 18363级别2013-09-01 08:07:41.350 ..:1 2 3 4 5 6 7 8 9 10 ...

$ TimeSensor:num 158489 158489 158490 158490 158491 ...



所以现在我只需要知道如何将时间和日期从因素转换为时间和日期。



如果有人知道解决方案,我将非常感激!我是一个关于R的noob,所以请不要把我烧到地上..



感谢一百万!

解决方案

对不起,这个迟到的答案!无论如何,我得到了大学的人的帮助,他想出了以下,非常简单的调整我的时间码..:

  df $ Date<  -  as.Date(df $ Start)#already从上面的答案得到这个
df $ Time< - format(as.POSIXct(df $ Start),format = %H:%M:%S)

将这些因素转换为date和 POSIXct,只是我想要的。



感谢大家的帮助!我希望未来能够回报某种恩惠,虽然我怀疑是否会与编程有关..!


I have a df with a column datetime (DD:MM:yyyy HH:mm:ss)named "Start" and I would like to split this column into two named "date" and "time".
Now I have tried the following:

df$Date <- sapply(strsplit(as.character(df$Start), " "), "[", 1)
df$Time <- sapply(strsplit(as.character(df$Start), " "), "[", 2)

This works, however, if I use the function str(df) (I cut it short so you can mostly see the variables of my concern).

'data.frame': 18363 obs. of 19 variables:
$ Start : Factor w/ 67 levels "2013-09-01 08:07:41.000",..: 1 1 1 1 1 1 1 1 1 1 ...
$ Interval : int 47259 47259 47259 47259 47259 47259 47259 47259 47259 47259 ...
$ DateTime : Factor w/ 18363 levels "2013-09-01 08:07:41.350",..: 1 2 3 4 5 6 7 8 9 10 ...
$ TimeSensor: num 158489 158489 158490 158490 158491 ...


So now I only need to know how to convert the time and date from 'factors' to 'time' and 'date'.

If someone knows the solution I would be very grateful! I am a noob concerning R so please do not burn me to the ground..

Thanks a million!

解决方案

Sorry for this late answer! Anyways, I got help from someone at the university and he came up with the following, very simple, adjustment of my time-code..:

df$Date <- as.Date(df$Start) #already got this one from the answers above
df$Time <- format(as.POSIXct(df$Start) ,format = "%H:%M:%S") 

This converts the factors to "date" and "POSIXct", just how I wanted it.

Thank all of you for your help! I hope I can return some kind of favour in the future, although I doubt if it will be with programming..!

这篇关于拆分日期时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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