[R] Date format

Rainer Hurling rhur||n @end|ng |rom gwdg@de
Sun May 10 08:22:06 CEST 2020


Am 10.05.20 um 04:17 schrieb Bert Gunter:
> $date is a factor, which is coded as numeric values internally, which
> as.date sees as numeric, and therefore:
> "as.Date will accept numeric data (the number of days since an epoch),
> but only if origin is supplied." (from ?as.Date)

as.Date is also able to read ISO date formatted strings by default (w/o
format).

as.Date("2013-04-28") works, as.Date("28.04.2013") not. The second
example needs as.Date("28.04.2013", format = "%d.%m.%Y").

> 
> You need to supply a format argument to as.Date to get it to handle
> the factor properly; e.g.
> "%d.%m.%Y"  should work. See ?strptime for formatting details.
> 
> Bert Gunter
> 
> "The trouble with having an open mind is that people keep coming along
> and sticking things into it."
> -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )
> 
> On Sat, May 9, 2020 at 5:31 PM Medic <mailiPadpost using gmail.com> wrote:
>>
>> I took a SAMPLE CODE (for Connected scatterplot) from the R gallery
>> and applied to MY DATA, but got:
>> "Error in as.Date.numeric(mydata$date) : 'origin' must be supplied".
>> P.S. I can not understand ?as.Date()
>>
>> SAMPLE CODE
>> library(ggplot2)
>> library(dplyr)
>> library(hrbrthemes)
>> data <- read.table("https://raw.githubusercontent.com/holtzy/data_to_viz/master/Example_dataset/3_TwoNumOrdered.csv",
>> header=T)
>>
>> str(data)
>> 'data.frame': 1822 obs. of  2 variables:
>>  $ date : chr  "2013-04-28" "2013-04-29" "2013-04-30" "2013-05-01" ...
>>  $ value: num  136 147 147 140 126 ...
>>
>> data$date <- as.Date(data$date)
>>
>> # Plot
>> data %>%
>>   tail(10) %>%
>>   ggplot( aes(x=date, y=value)) +
>>     geom_line( color="grey") +
>>     geom_point(shape=21, color="black", fill="#69b3a2", size=6) +
>>     theme_ipsum() +
>>     ggtitle("Evolution of bitcoin price")
>>
>>
>> MY DATA
>> mydata <- read.table("E:/mydata.csv", header=TRUE, sep=";", dec=",")
>>
>> str(mydata)
>> 'data.frame': 7 obs. of  2 variables:
>>  $ date : chr  "01.01.2000" "02.01.2000" "03.01.2000" "04.01.2000" ...
>>  $ value: int  11 12 13 14 15 16 17
>>
>> mydata$date <- as.Date(mydata$date)
>> Error in as.Date.numeric(mydata$date) : 'origin' must be supplied
>>
>> ______________________________________________
>> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
> 
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



More information about the R-help mailing list