English 中文(简体)
从日期和小时格式改为数字格式
原标题:Change from date and hour format to numeric format
  • 时间:2011-11-21 16:51:02
  •  标签:
  • r
  • date
  • time

我正在R中工作,我需要从格式一栏改为一栏。

9/27/2011  3:33:00 PM 

a 价值格式。 在Excel I中,可以使用 Value(),但我不知道如何在R中这样做。

我的数据表明:

9/27/2011 15:33 a   1   5   9
9/27/2011 15:33 v   2   6   2
9/27/2011 15:34 c   3   7   1
问题回答

为了将体格转换为R日期格式,使用as.POSIXct——那么,你可以使用as.全数<>强制其为数字数值。

> x <- as.POSIXct("9/27/2011  3:33:00 PM", format="%m/%d/%Y  %H:%M:%S %p")
> x
[1] "2011-09-27 03:33:00 BST"
> as.numeric(x)
[1] 1317090780

你得到的数值表明,自任意日期(通常为1970年1月1日)以来,第二位数。 请注意,这与Excel不同,因为一个日期是自任意日期以来的天数(1/1/1900,如果我的记忆为我服务的话——我试图不再使用Excel。)

详情见? 日期Classes

这对我有用:

> test=as.POSIXlt("09/13/2006", format="%m/%d/%Y")
> test
[1] "2006-09-13"
> 1900+test$year
[1] 2006
> test$yday
[1] 255
> test$yday/365
[1] 0.6986301
> 1900+test$year+test$yday/366
[1] 2006.697

如果你们需要像在Excel那样的日数,你可以采取类似的做法。





相关问题
Mysql compaire two dates from datetime?

I was try to measure what is faster and what should be the best way to compare two dates, from datetime record in MySql db. There are several approaches how to grab a date from date time, but I was ...

iPhone Date Picker rolls over to 2010 on December 27th?

So I implemented a UIDatepicker in one of my applications for scheduling and autodialing teleconferences... everything is pretty much ready to go except, while testing I noticed that when the date ...

Convert date Python

I have MMDDYY dates, i.e. today is 111609 How do I convert this to 11/16/2009, in Python?

specifying date format when using $form->inputs() in CakePHP

I am wondering if there is a way to specify the date format in the forms created using CakePHP s $form->inputs(); Please note that this is not the individual $form->input() but instead $form->inputs() ...

NSDateFormat, super simple! Where am I screwing up?

Ok, this is really simple, maybe I m a getting a bit burnt out, but seems like it should work, Query XML feed, put out date string, format, display in a cell. The issue is I m a getting a NULL ...

sqlite writing a date into an email

I am exporting a date value from sqlite and placing it into an email. The date appears like this 279498721.322872 I am using Objective C in an Iphone App. Does anyone know how to make this export ...