Friends of stack overflow , I am having a heck of a time getting my fake data.frame to convert to class type 'date'.
library(anytime)
fake.data<-data.frame(
date = c('01/01/2019', '01/02/2019', '01/03/2019', '01/04/2019', '01/05/2019', '01/06/2019', '01/07/2019',
'01/01/2019', '01/02/2019', '01/03/2019', '01/04/2019', '01/05/2019', '01/06/2019', '01/07/2019'),
location = c('Point A', 'Point A', 'Point A', 'Point A', 'Point A', 'Point A', 'Point A',
'Point B', 'Point B', 'Point B', 'Point B', 'Point B', 'Point B', 'Point B'
),
vehicle = c('ZZ12', 'ZZ12', 'AA12', 'AA12', 'AA12', 'AA12', 'ZZ12',
'ZZ12', 'ZZ12', 'AA12', 'AA12', 'AA12', 'AA12', 'ZZ12'),
count = c(2, 1, 4, 4, 3, 4, 2,
3, 3, 1, 1, 5, 6, 6),
stringsAsFactors = FALSE)
The structure returns:
>str(fake.data$date)
chr [1:14] "01/01/2019" "01/02/2019" "01/03/2019" "01/04/2019" "01/05/2019" "01/06/2019" "01/07/2019" "01/01/2019" "01/02/2019" ...
My attempts to change the class type to 'Date' continue to fail. For example:
fake.data$date<- anydate(fake.data$date)
Returns:
> head(str(fake.data))
'data.frame': 14 obs. of 4 variables:
$ date : Date, format: "2019-01-01" "2019-01-02" "2019-01-03" "2019-01-04" ...
$ location: chr "Point A" "Point A" "Point A" "Point A" ...
$ vehicle : chr "ZZ12" "ZZ12" "AA12" "AA12" ...
$ count : num 2 1 4 4 3 4 2 3 3 1 ...
This seems great, but when I try to utilize this for visualization (ie plots), I get what I think are as.POSIXct:
The date no longer renders in the format..just changes in to this odd numeric. Any ideas?
I have also tried as.Date
, as.character(as.Date(...))
, to no avail. Oddly..the date on the bottom of the chart still renders the correct format.
ui<- shinyUI(
fluidPage(
plotOutput("plotthis", hover="clickthis"),
tableOutput("rawdata")
)
)
server<- shinyServer(function(input,output) {
output$plotthis<- renderPlot({
ggplot(fake.data,aes(x=date, y=vehicle)) +
geom_point()
})
output$rawdata<- renderTable({
nearPoints(fake.data,input$clickthis, threshold = 10)
})
})
shinyApp(ui, server)
Try this :
library(lubridate)
fake.data$new_date <- dmy(fake.data$date)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.