我如何使用R中的as.Date()格式化我的日期? [英] How can I format my Dates using as.Date() in R?
问题描述
我有一个如下所示的数据集:
日期AE AA AEF百分比
1/1/2012 1211 1000 3556 0.03
1/2/2012 100 2000 3221 0.43
1/3/2012 3423 10000 2343 0.54
1/4/2012 10000 3000 332 0.43
1/5 / 2012 2342 500 4435 0.43
1/6/2012 2342 800 2342 0.23
1/7/2012 2342 1500 1231 0.12
1/8/2012 111 2300 333
1 / 9/2012 1231 1313 3433
2012年1月1日3453 5654 222
2012年1月1日3453 3453 454
2012年1月12日5654 7685 3452
我试图用ggplot来绘制这个集合,但ggplot没有按顺序绘制日期,因为它们不是数字。我尝试使用as.Date()来转换日期。
library(ggplot2)
data< - read.csv(GCdataViz / test2.csv)
newDates< - as.Date(data $ Date)
ggplot(data,aes(x = newDates,y = Percent))+
geom_point(size = 3)
然而,日期图不是我预期的那样。尽管数据集全是1月份的数据(mm / dd / yyyy),但我在ggplot中看到了不同的月份。
任何人都可以重现并诊断问题吗?谢谢。
阅读 help(as.Date)
- you还需要提供格式字符串:
R> as.date(c(1/1/2001,1/2/2001,1/3/2001),%m /%d /%Y)
[1] 2001-01-012001-01-022001-01-03
R>
I have a dataset that looks like this:
Date AE AA AEF Percent
1/1/2012 1211 1000 3556 0.03
1/2/2012 100 2000 3221 0.43
1/3/2012 3423 10000 2343 0.54
1/4/2012 10000 3000 332 0.43
1/5/2012 2342 500 4435 0.43
1/6/2012 2342 800 2342 0.23
1/7/2012 2342 1500 1231 0.12
1/8/2012 111 2300 333
1/9/2012 1231 1313 3433
1/10/2012 3453 5654 222
1/11/2012 3453 3453 454
1/12/2012 5654 7685 3452
I am trying to plot this set with ggplot, but ggplot does not plot the dates in order because they are not numeric. I am trying to convert the dates using as.Date().
library(ggplot2)
data <- read.csv("GCdataViz/test2.csv")
newDates <- as.Date(data$Date)
ggplot(data, aes(x = newDates, y = Percent)) +
geom_point(size = 3)
However, the date plots are not what I expected them to be. Whereas the dataset is all January data (mm/dd/yyyy), I see different months in the ggplot.
Can anyone reproduce this and diagnose the problem? Thanks.
Read help(as.Date)
-- you need to supply a format string as well:
R> as.Date(c("1/1/2001", "1/2/2001", "1/3/2001"), "%m/%d/%Y")
[1] "2001-01-01" "2001-01-02" "2001-01-03"
R>
这篇关于我如何使用R中的as.Date()格式化我的日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!