Home > OS >  How to convert longt text date format to simple? [duplicate]
How to convert longt text date format to simple? [duplicate]

Time:09-29

I just want to convert date from "dbY hms" to "y-m-d".

This is my data:

                  Time    X10cm    X20cm    X30cm    X40cm    X50cm    X60cm    X70cm    X80cm    X90cm
1 05 Apr 2019 09:46:13 20.70675 26.20419 23.66370 18.04151 3.507654 5.644918 3.947458 0.926415 1.304021
2 11 Apr 2019 08:36:32 18.45716 25.76273 23.82202 18.59679 3.829793 6.639636 4.313009 1.002555 1.440603
3 19 Apr 2019 09:24:16 17.22486 24.03394 21.70397 16.95699 3.507654 6.827912 4.417910 1.046471 1.574125
4 26 Apr 2019 12:14:05 16.60325 22.16044 19.54150 15.73587 3.237127 6.169124 3.987147 1.002555 1.397690
5 10 May 2019 07:40:20 19.68528 22.46739 19.20813 14.97607 3.184556 5.620616 3.888364 0.959796 1.484311
6 17 May 2019 12:07:31 16.82389 23.13976 20.70675 16.86820 3.470846 5.500014 3.714201 0.985313 1.429803

I have tried many things, for example creating a character to transform it to date:

data2 = data1 %>%
  mutate(Time1 = format(Time, format = "%Y-%m-%d %H:%M:%S")) %>%
  mutate_at("Time1",as.Date,format="%Y-%m-%d")

but it didn't work.

CodePudding user response:

You may use -

x <- '05 Apr 2019 09:46:13'
as.Date(x, '%d %b %Y')
#[1] "2019-04-05"

For the entire column of the dataframe.

data1$Time <-  as.Date(data1$Time, '%d %b %Y')

If you want to use dplyr and lubridate.

library(dplyr)
library(lubridate)

data1 <- data1 %>% mutate(Time = as.Date(dmy_hms(Time)))
  • Related