将日期数据(m / d / y)分割成3个单独的列 [英] Split date data (m/d/y) into 3 separate columns

查看:148
本文介绍了将日期数据(m / d / y)分割成3个单独的列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将日期(m / d / y格式)转换为3个单独的列,我希望运行一个算法(我正在将我的日期转换为朱利安日数)。看到这个建议,另一个用户使用Oracle将数据分离成多个列。我正在使用R,并且我完全坚持如何适当地编码。 A1,A2 ...代表我的新列标题,更新集部分的格式差异是什么?

  update< tablename>设置A1 = substr(ORIG,1,4),
A2 = substr(ORIG,5,6),
A3 = substr(ORIG,11,6),
A4 = substr ORIG,17,5);

我正在努力提高我在R中的技能,但无法想象这个...任何帮助非常感激感谢提前...:)

解决方案

给定一个文本变量x,像这样:

 > x 
[1]10/3/2001

then:

 > as.Date(x,%m /%d /%Y)
[1]2001-10-03

将其转换为日期对象。那么,如果你需要它:

 > julian(as.Date(x,%m /%d /%Y))
[1] 11598
attr(,origin)
[1]1970-01 -01

给你一个朱利安日期(相对于1970-01-01)。



不要尝试子串的东西...



请参阅help(as.Date)了解更多信息。 >

I need to convert date (m/d/y format) into 3 separate columns on which I hope to run an algorithm.(I'm trying to convert my dates into Julian Day Numbers). Saw this suggestion for another user for separating data out into multiple columns using Oracle. I'm using R and am throughly stuck about how to code this appropriately. Would A1,A2...represent my new column headings, and what would the format difference be with the "update set" section?

 update <tablename> set A1 = substr(ORIG, 1, 4), 
                       A2 = substr(ORIG, 5, 6), 
                       A3 = substr(ORIG, 11, 6), 
                       A4 = substr(ORIG, 17, 5); 

I'm trying hard to improve my skills in R but cannot figure this one...any help is much appreciated. Thanks in advance... :)

解决方案

Given a text variable x, like this:

> x
[1] "10/3/2001"

then:

> as.Date(x,"%m/%d/%Y")
[1] "2001-10-03"

converts it to a date object. Then, if you need it:

> julian(as.Date(x,"%m/%d/%Y"))
[1] 11598
attr(,"origin")
[1] "1970-01-01"

gives you a Julian date (relative to 1970-01-01).

Don't try the substring thing...

See help(as.Date) for more.

这篇关于将日期数据(m / d / y)分割成3个单独的列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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