【发布时间】:2014-11-14 14:04:25
【问题描述】:
我在 Gnu R 中处理时间变量时遇到了两个问题!
首先,我无法使用 as.Posixlt 或 as.Date 从因子(或字符)重新编码时间数据(可下载 here)而不会出现错误消息像这样:
字符串不是标准的明确格式
然后我尝试使用以下方法隐藏我的时间数据:
dates <- strptime(time, "%Y-%m-%j")
这只给了我:
不适用
其次,我想(不得不)转换我的时间数据的原因是我想用 ggplot2 绘制它并调整我的 scale_x_continuous (如here 所述)以便它只写我每 50 年(即 1250-01-01、1300-01-01 等)在 x 轴上,否则 x 轴太忙(见下图)。
这是我使用的代码:
library(ggplot2)
library(scales)
library(reshape)
df <- read.csv(file="https://dl.dropboxusercontent.com/u/109495328/time.csv")
attach(df)
dates <- as.character(time)
population <- factor(Number_Humans)
ggplot(df, aes(x = dates, y = population)) + geom_line(aes(group=1), colour="#000099") + theme(axis.text.x=element_text(angle=90)) + xlab("Time in Years (A.D.)")
【问题讨论】:
-
“1250-02-46”是什么日期? 2 月的天数很少超过 29 天。另外,不要使用
attach。 -
不建议使用
attach? 46 是这种情况下是一年中的第 46 天。这不是我做的,而是我使用的程序。这对 R 来说真的是个问题吗? -
你的意思是,如果你通过 R 一些东西,你告诉它“46”是“作为十进制数的月份中的日期 (01–31)”,它实际上不是月份中的日期, R会感到困惑吗?令人震惊的是,是的。您需要 %j,即“十进制数 (001–366) 中的日期”。
-
呵呵;您正在使用 %j。如果我运行它,gist.github.com/Ironholds/12097162d25dc935960e 是输出。你不会这样吗?
-
您的时间戳似乎具有三个引用级别。这些引号是否包含在值中?