correctly set date on csv excel file

Good morning everyone,
I am a rstudio beginner.
I want to do a time series analysis and forecast sales in the future.
I have created an excel csv file with the months of the year in one column and the relative monthly sales in quantity of the product in another column. From 2015 to 2021.
The file I import it correctly on rstudio cloud but I think I have set the dates wrong because when I use the plot command it gives me the following error:
"
Error in plot.window(...) : need finite 'xlim' values
In addition: Warning messages:
1: In xy.coords(x, y, xlabel, ylabel, log) : NAs introduced by coercion
2: In min(x) : no non-missing arguments to min; returning Inf
3: In max(x) : no non-missing arguments to max; returning -Inf
"
the format of the dates is as follows: 01/01/2015

Can someone help me?
I think the error how i write the date because if i do summary() rstudio says that is charachter and not data

Welcome to the forum.

It looks like you will need to convert from character to date format but we really need to see some sample data

I think we need a FAQ: How to do a minimal reproducible example ( reprex ) for beginners

or
A handy way to supply some sample data is the dput() function. In the case of a large dataset something like dput(head(mydata, 100)) should supply the data we need.

Also is your date in day/month/year format or month/day/year format?

Hello jrkrideau,

"It looks like you will need to convert from character to date format"

No, i m sure the format is correct: for exemple 01/01/2015 has data format and not for exemple general format

Also is your date in day/month/year format or month/day/year format?"

I tried in both ways, now i try again. What s your advice of data format?

structure(list(mese = c("01/01/15", "01/02/15", "01/03/15", "01/04/15",
"01/05/15", "01/06/15", "01/07/15", "01/08/15", "01/09/15", "01/10/15",
"01/11/15", "01/12/15", "01/01/16", "01/02/16", "01/03/16", "01/04/16",
"01/05/16", "01/06/16", "01/07/16", "01/08/16", "01/09/16", "01/10/16",
"01/11/16", "01/12/16", "01/01/17", "01/02/17", "01/03/17", "01/04/17",
"01/05/17", "01/06/17", "01/07/17", "01/08/17", "01/09/17", "01/10/17",
"01/11/17", "01/12/17", "01/01/18", "01/02/18", "01/03/18", "01/04/18",
"01/05/18", "01/06/18", "01/07/18", "01/08/18", "01/09/18", "01/10/18",
"01/11/18", "01/12/18", "01/01/19", "01/02/19", "01/03/19", "01/04/19",
"01/05/19", "01/06/19", "01/07/19", "01/08/19", "01/09/19", "01/10/19",
"01/11/19", "01/12/19", "01/01/20", "01/02/20", "01/03/20", "01/04/20",
"01/05/20", "01/06/20", "01/07/20", "01/08/20", "01/09/20", "01/10/20",
"01/11/20", "01/12/20", "01/01/21", "01/02/21", "01/03/21", "01/04/21",
"01/05/21", "01/06/21", "01/07/21", "01/08/21", "01/09/21", "01/10/21",
"01/11/21", "01/12/21"), quantita = c(3118L, 2944L, 2776L, 1369L,
744L, 518L, 1918L, 271L, I DELETE SOME DATA HERE...)), row.names = c(NA, 84L), class = "data.frame")

thank you for all,

you are really kind

(post deleted by author)

Here is an example of one method for changing the characters in your data frame into dates.

DF <- structure(list(mese = c("01/01/15", "01/02/15", "01/03/15", "01/04/15",
                              "01/05/15", "01/06/15", "01/07/15", "01/08/15", "01/09/15", "01/10/15",
                              "01/11/15", "01/12/15", "01/01/16", "01/02/16", "01/03/16", "01/04/16",
                              "01/05/16", "01/06/16", "01/07/16", "01/08/16", "01/09/16", "01/10/16",
                              "01/11/16", "01/12/16", "01/01/17", "01/02/17", "01/03/17", "01/04/17",
                              "01/05/17", "01/06/17", "01/07/17", "01/08/17", "01/09/17", "01/10/17",
                              "01/11/17", "01/12/17", "01/01/18", "01/02/18", "01/03/18", "01/04/18",
                              "01/05/18", "01/06/18", "01/07/18", "01/08/18", "01/09/18", "01/10/18",
                              "01/11/18", "01/12/18", "01/01/19", "01/02/19", "01/03/19", "01/04/19",
                              "01/05/19", "01/06/19", "01/07/19", "01/08/19", "01/09/19", "01/10/19",
                              "01/11/19", "01/12/19", "01/01/20", "01/02/20", "01/03/20", "01/04/20",
                              "01/05/20", "01/06/20", "01/07/20", "01/08/20", "01/09/20", "01/10/20",
                              "01/11/20", "01/12/20", "01/01/21", "01/02/21", "01/03/21", "01/04/21",
                              "01/05/21", "01/06/21", "01/07/21", "01/08/21", "01/09/21", "01/10/21",
                              "01/11/21", "01/12/21")), row.names = c(NA, 84L), class = "data.frame")
str(DF)
#> 'data.frame':    84 obs. of  1 variable:
#>  $ mese: chr  "01/01/15" "01/02/15" "01/03/15" "01/04/15" ...
DF$mese <- as.Date(DF$mese,format = "%d/%m/%y")
str(DF)
#> 'data.frame':    84 obs. of  1 variable:
#>  $ mese: Date, format: "2015-01-01" "2015-02-01" ...

Created on 2022-08-29 by the reprex package (v2.0.1)
It is important to keep in mind that a csv file is just plain text. The format that Excel uses when it opens and interprets the file is not relevant to how R will handle the file. Since a date with the format 01/02/15 is ambiguous, it could be January 2nd or February 1st, R imports it as characters.

1 Like

Thank you a lot.

Now it works!

Have a good day,

Federico

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.