我需要将日期(m/d/y格式)转换为3个单独的列,我希望在这些列上运行一个算法。(我正在尝试将日期转换为Julian Day数字)。我为另一位用户提供了使用Oracle将数据分成多列的建议。我使用的是R,并且完全不知道如何正确地编写代码。我的新列标题是否会更新,以及“A2...represent set”部分的格式有何不同?
update <tablename> set A1 = substr(ORIG, 1, 4),
A2 = substr(ORIG, 5, 6),
A3 = substr(ORIG, 11, 6),
A4 = substr(ORIG, 17, 5); 我正在努力提高我的R技能,但不知道这个one...any帮助是很受欢迎的。先谢谢你...:)
发布于 2010-11-02 22:29:02
给定一个文本变量x,如下所示:
> x
[1] "10/3/2001"然后:
> as.Date(x,"%m/%d/%Y")
[1] "2001-10-03"将其转换为date对象。然后,如果你需要它:
> julian(as.Date(x,"%m/%d/%Y"))
[1] 11598
attr(,"origin")
[1] "1970-01-01"为您提供一个儒略日期(相对于1970-01-01)。
不要尝试子字符串的事情...
有关更多信息,请参阅帮助(as.Date)。
发布于 2010-11-03 00:42:58
我使用Date对象的format()方法来拆分R中的日期。使用Dirk的datetext,下面是我如何将日期拆分成其组成部分的方法:
datetxt <- c("2010-01-02", "2010-02-03", "2010-09-10")
datetxt <- as.Date(datetxt)
df <- data.frame(date = datetxt,
year = as.numeric(format(datetxt, format = "%Y")),
month = as.numeric(format(datetxt, format = "%m")),
day = as.numeric(format(datetxt, format = "%d")))这就给出了:
> df
date year month day
1 2010-01-02 2010 1 2
2 2010-02-03 2010 2 3
3 2010-09-10 2010 9 10请注意其他几个人所说的话;您可以获得Julian日期,而无需拆分各种日期组件。我添加这个答案是为了向您展示,如果您需要它来做其他事情,如何进行拆分。
发布于 2010-11-02 22:20:49
快速提示:
base R中已经存在
help(julian).下面是一个示例:
datetxt <- c("2010-01-02", "2010-02-03", "2010-09-10")
dates <- as.Date(datetxt) ## you could examine these as well
plt <- as.POSIXlt(dates) ## now as POSIXlt types
plt[["year"]] + 1900 ## years are with offset 1900
#[1] 2010 2010 2010
plt[["mon"]] + 1 ## and months are on the 0 .. 11 intervasl
#[1] 1 2 9
plt[["mday"]]
#[1] 2 3 10
df <- data.frame(year=plt[["year"]] + 1900,
month=plt[["mon"]] + 1, day=plt[["mday"]])
df
# year month day
#1 2010 1 2
#2 2010 2 3
#3 2010 9 10当然还有
julian(dates)
#[1] 14611 14643 14862
#attr(,"origin")
#[1] "1970-01-01"https://stackoverflow.com/questions/4078502
复制相似问题