I want to represent a graph with dates on the x-axis and prices on the y-axis. When I import the CVS document, I have no problem. Each type of information is in its column, and the dates are in a suitable format. But when I try to graph this data. I get non-conforming lines, and the date format is not the same (now: 0002, 0004, 0006, 0008, 0010, etc...). What can I do?
I thank you in advance.
Here is my code and the pictures: #Put the data in a matrix p
p <-read.csv("JSE_Price_index_and_Total_Returns1.csv", header = TRUE , sep = ",")
Your 'dates' are obviously not the type and format that R recognizes as dates.
Show us a little part of your data. For example with head(p) that shows the first six rows.
I think that there is a problem with your format.
Probably your Date is not numerical but character and the as.Date comversion guesses the wrong order of days, months and years.
The lubridate package helps with handling all kind of date conversion.
See the following example where I think you will want to do the second conversion:
p <- data.frame(
stringsAsFactors = F,
Date = c("06/05/2006","06/06/2006","06/07/2006"),
Price_Index = c(100,90.7,86.2),
Tot_Ret_Index = c(100,90.68,86.19)
)
p$Date1 = lubridate::dmy(p$Date)
p$Date2 = lubridate::parse_date_time(p$Date,"%m/%d/%Y")
head(p)
#> Date Price_Index Tot_Ret_Index Date1 Date2
#> 1 06/05/2006 100.0 100.00 2006-05-06 2006-06-05
#> 2 06/06/2006 90.7 90.68 2006-06-06 2006-06-06
#> 3 06/07/2006 86.2 86.19 2006-07-06 2006-06-07
plot(p$Date1,p$Price_Index, type = "p", col="black", lwd = 1)
plot(p$Date2,p$Price_Index, type = "p", col="black", lwd = 1)
Created on 2021-07-15 by the reprex package (v2.0.0)
However, my data table contains almost 1585 observations.
So how can I use the lubridate package without putting one by one the data in the "c" vectors?
I want to be able to convert all the elements in my data frame to numeric.
because I do not have your data file, I had to prepare an example data.frame myself.
In your case you can use the data.frame that you have read from the csv-file,
so no need to type in any data.
I tried it but it doesn't work. I get a linear line and the date are no more in the x abscisse(because I want the prices in the x abscisse):
I did the following code:
#Put the data in a matrix p
p <-read.csv("JSE_Price_index_and_Total_Returns1.csv", header = TRUE , sep = ",")
you are confusing me.
You show me some code and therefore I suppose that you have executed it.
For instance you did rename the names of the columns and after that you show the first lines with head.
The result shows the names as Date Price Index Total Return Index Date2
When I do the same I get
and I would except to see the same names in your output. So something is wrong.
I notice that you are editing the code and its output in this conversation and its easy (and therefore confusing) to make a mistake in that. The best way to avoid that is just copying the contents of the R-console into the window where you pose or answer questions . When you use three backticks (`) before and after your copied text it will be correctly readable.
Also read about the possibilities of the reprex package. See e.g. here
I second the suggestion of @nirgrahamuk for a good introductory text.
Thanks for your message.
I'm sorry to disturb you with my code. Thanks for the advice. I will read the document presented by
nirgrahamuk. Yes, before I tell you if the code works or not, I execute it.
But @HanOostdijk, I don't understand very well. By renaming the lines. I get the same result as you.
I think that the error is rather in the code to represent the graph.
I get this : (same as you)