如何使用R更改数据框中的日期格式

时间:2015-10-14 04:24:42

标签: r date graph ggplot2 format

我想在R中绘制n(y轴)与日期(x轴)的关系图,但由于我的数据中显示的日期格式,日期的顺序不正确升序。我怎么解决这个问题?感谢您的帮助。

hybrid <- readWorksheetFromFile(excel.file, sheet="ResultSet", header=TRUE)
wb <- loadWorkbook(excel.file)
setMissingValue(wb,value=c("NA"))
hybrid1 <- readWorksheet(wb, sheet="ResultSet", header=TRUE)

我使用了dplyr函数。假设每个Pub.Number都有一个唯一的代码&amp;我用一个替换它。然后,我计算某个日期的数量。

hybrid <- mutate(hybrid1, n=sum(Publication.Number=1))
p1 <- select(hybrid1, Publication.Date, n)
pt <- count(p1, Publication.Date, wt=n)

输出如下:

pt
Source: local data frame [627 x 2]

   Publication.Date     n
              (chr) (dbl)
1        01.01.2013     1
2        01.01.2014     8
3        01.01.2015    10
4        01.02.2012     3
5        01.03.2012    16
6        01.04.2015     2
7        01.05.2012     1
8        01.05.2013     7
9        01.05.2014    23
10       01.06.2011     1
..              ...   ...

然后,我绘制了它,但R将Pub.Date识别为角色

qplot(x=Publication.Date, y=n, data=pt, geom="point")

x <- hybrid1[,2]
class(x)
[1] "character"

The graph I've plotted is a mess because of the wrong order of the date

我尝试使用as.Date函数,但似乎它不完整(我使用的是R版本3.2.2)

> pt[,1] <- as.Date(pt[,1], format='%d.%m.%Y’)
+

2 个答案:

答案 0 :(得分:0)

在通常使用R输入数据的过程中,像&#34; 2013年1月1日&#34;将成为因子变量。由于它们不是两种标准日期格式之一:YYYY / MM / DD或YYYY-MM-DD,因此无法直接输入&#34;日期&#34; s&#34; colClasses& #34;除非你建立一个&#34; as.DT&#34;方法。您需要确保它们是字符向量,方法是在读取函数中使用stringsAsFactors=FALSE,或者在输入后使用as.character强制转换为字符。你显示的那个标题让我觉得这个数据已经被操作了dsomehow,也许是dplyr包中的函数?

res <- structure(list(Publication.Date = structure(1:10, .Label = c("01.01.2013", 
 "01.01.2014", "01.01.2015", "01.02.2012", "01.03.2012", "01.04.2015", 
 "01.05.2012", "01.05.2013", "01.05.2014", "01.06.2011"), class = "factor"), 
     n = c(1L, 8L, 10L, 3L, 16L, 2L, 1L, 7L, 23L, 1L)), .Names = c("Publication.Date", 
 "n"), class = "data.frame", row.names = c("1", "2", "3", "4", 
 "5", "6", "7", "8", "9", "10"))

> res
   Publication.Date  n
1        01.01.2013  1
2        01.01.2014  8
3        01.01.2015 10
4        01.02.2012  3
5        01.03.2012 16
6        01.04.2015  2
7        01.05.2012  1
8        01.05.2013  7
9        01.05.2014 23
10       01.06.2011  1

> res$Publication.Date <- as.Date( as.character(res$Publication.Date), format="%m.%d.%Y")

然后你可以绘制:

png();  qplot(x=Publication.Date, y=n, data=res, geom="point"); dev.off()

enter image description here

答案 1 :(得分:0)

首先将'Publication.Date'转换为Date格式,然后订购:

使用您的数据:

data <- read.table(pipe('pbpaste'),sep='',header=T,stringsAsFactors = F)
  data <- data[,-1]
  names(data) <- c('Pub.Date', 'n’)

    Pub.Date  n
1 01.01.2014  8
2 01.01.2015 10
3 01.02.2012  3
4 01.03.2012 16
5 01.04.2015  2
6 01.05.2012  1
7 01.05.2013  7
8 01.05.2014 23
9 01.06.2011  1

将'Pub.Date'转换为日期格式:

data[,1] <- as.Date(data[,1],format='%d.%m.%Y’)

并订购:

data[order(data$"Pub.Date",data$n), ]

    Pub.Date  n
9 2011-06-01  1
3 2012-02-01  3
4 2012-03-01 16
6 2012-05-01  1
7 2013-05-01  7
1 2014-01-01  8
8 2014-05-01 23
2 2015-01-01 10
5 2015-04-01  2