Frage

I need to plot a time series in R. But having a tough time to figure out how to work with the formats.

dates <- c(20060901, 20060905, 20060906, 20060907, 20060908, 20060911)
values <- c(33.6, 32.0, 30.0, 30.0, 30.0, 28.4)

Need to convert the dates in proper Format E.g: 20060901 to Sep-01-2006 and so on

My approach so far:

dates = as.Date(dates, "%Y%m%d")
plot(dates , values)

However, I get the following error :

Error in charToDate(x) : character string is not in a standard unambiguous format
War es hilfreich?

Lösung

You probably want to read some basic docs to learn more about basic types, but here is a worked example using your data:

R> data <- data.frame(dates=as.Date(as.character(c(20060901,20060905,20060906, 
+                                                  20060907,20060908,20060911)), 
+                                   "%Y%m%d"), 
+                     values=c(33.6, 32.0, 30.0, 30.0, 30.0, 28.4))
R> data
       dates values
1 2006-09-01   33.6
2 2006-09-05   32.0
3 2006-09-06   30.0
4 2006-09-07   30.0
5 2006-09-08   30.0
6 2006-09-11   28.4
R> class(data[,1])
[1] "Date"
R> plot(data)

which yields

enter image description here

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top