堆叠之友溢出, 我真想把我的假data.frame转换为类类型'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)
结构返回:
>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" ...
我将类类型更改为“日期”的尝试继续失败。例如:
fake.data$date<- anydate(fake.data$date)
返回:
> 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 ...
这看起来很棒,但是当我尝试将其用于可视化(即绘图)时,我得到了我认为的样子。POSIXct:
日期不再以格式显示。仅更改为该奇数。有什么想法吗?
我也尝试了as.Date
,as.character(as.Date(...))
,但无济于事。奇怪的是,图表底部的日期仍然呈现正确的格式。
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)
答案 0 :(得分:1)
尝试一下:
library(lubridate)
fake.data$new_date <- dmy(fake.data$date)