如何使用 R 更改数据框中的日期格式
How to change the date format in my data frame using R
我想在 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 都有一个唯一的代码,我用一个替换了它。然后,我计算它在某个日期的数量。
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’)
+
在通常的R输入数据过程中,"01.01.2013"这样的值会成为因子变量。因为它们不在两个 "stadard Date formats: YYYY/MM/DD or YYYY-MM-DD, they cannot be input directly as "Date"s with "colClasses 之一中,除非您构建 "as.DT" 方法。您需要通过使用 [=12= 来确保它们是字符向量] 在读取函数中或在输入后用 as.character
强制转换为字符。你显示的 header 让我觉得这个数据已经被以某种方式处理过,也许是 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()
先将'Publication.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
我想在 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 都有一个唯一的代码,我用一个替换了它。然后,我计算它在某个日期的数量。
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’)
+
在通常的R输入数据过程中,"01.01.2013"这样的值会成为因子变量。因为它们不在两个 "stadard Date formats: YYYY/MM/DD or YYYY-MM-DD, they cannot be input directly as "Date"s with "colClasses 之一中,除非您构建 "as.DT" 方法。您需要通过使用 [=12= 来确保它们是字符向量] 在读取函数中或在输入后用 as.character
强制转换为字符。你显示的 header 让我觉得这个数据已经被以某种方式处理过,也许是 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()
先将'Publication.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