Convert String of anyformat into dd-mm-yy hh:mm:ss in R

前端 未结 3 602
礼貌的吻别
礼貌的吻别 2020-12-07 04:05

I am reading a date value from csv file.So the format will vary according to the date format of csv. How can I convert any date string to dd-mm-yyy HH:mm:ss ?

ED

3条回答
  •  长情又很酷
    2020-12-07 04:28

    Try "lubridate" package. Here A_1.csv has both formats.

    Data <-read.csv("Al_1.csv") # import data
    str(Data)
    a = NULL # create a null object
    library(lubridate)
    a$Date <- mdy_hm(Data$Date) # store dd/mm/yyyy HH:mm:ss objects here
    a$Price <- Data$Price # get respective values(it could by any other column)
    b = NULL
    b$Date <- mdy(Data$Date)
    b$Price <- Data$Price
    
    a <- as.data.frame(a)
    b <- as.data.frame(b)
    
    
    a <- a[is.na(a$Date)==FALSE,] # those with NA had diffrent formats remove it
    b <- b[is.na(b$Date)==FALSE,]
    
    b$Date <- as.POSIXlt(b$Date)# change your other format also to UTC
    
    
    x <- rbind(a,b)
    str(x)
    x <- ts(x)
    

提交回复
热议问题