convert character to date and time

Hi.
I have a data frame where date is mentioned in character, how can convert it to date format.

 typeof(phen1$DATE)
[1] "character"

date format is (10/25/2013) i also want this to change to (25/10/2013) format.

i have one more date column that is also in character type.

format looks like ( 10/31/13 7:13 ) i want to change this also to date type.

My data data contains time column as well, which is in character type.
format looks like (18:15) , i want to change this to time type.( 24 hrs time format ) .

please help me to do this. Thank you very much

Have a look here: https://lubridate.tidyverse.org/ to work with time /dates (cheatsheet is very hand). If you need more help provide some data/reprex :slight_smile:

Data i have

time <- c("8H 0m 0s", " 18H 6M 0S", " 16H 30M 0S")
ID   <- c(1,2,3)
df <- data.frame(ID,time)

Data i want

ID   <- c(1,2,3)
time2 <- c("08:00", "18:06", "16:30")
df2 <- data.frame(ID,time2)

please help me to convert that. Thank you

library(lubridate)
library(tidyverse)
library(hms)


time <- c("8H 0m 0s", " 18H 6M 0S", " 16H 30M 0S")
ID   <- c(1,2,3)
df <- data.frame(ID,time)

#exctracting the numbers
df2 <- regmatches(time, gregexpr("[[:digit:]]+", time)) %>% unlist() %>% as.numeric()

#taking the vector and converting it into a matrix and then dataframe
df3 <- matrix(df2,ncol = 3) %>% t() %>% as.data.frame()

#giving it some proper names and binding it with the original
names(df3) <- c("h","m","s")
df3 <- cbind(df,df3)

#creating the new variable where we take the number of hours and minutes and express it as total number of seconds
df4 <- df3 %>% mutate(time2 = hms::as_hms((h*60*60)+(m*60)))

df4
#>   ID        time  h  m s    time2
#> 1  1    8H 0m 0s  8  0 0 08:00:00
#> 2  2   18H 6M 0S 18  6 0 18:06:00
#> 3  3  16H 30M 0S 16 30 0 16:30:00

Created on 2020-11-25 by the reprex package (v0.3.0)

Just note that you will have to change ncol = 3 if you have a much larger file (base it on the length of ID. There might be an easier way to accomplish this but seems to work relatively well (I didn't clean up all the steps).

1 Like

Thank you very much for your time and support.

Most welcome :slight_smile: If it solves your problem feel free to mark that post as the solution.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.

it worked well to concert date type and format, but when it comes to time it converts the type as below mentioned type

str(phen1)
'data.frame':	4038 obs. of  22 variables:
 $ ID    : chr  "1" "1" "1" "1" ...
 $ DATE  : Date, format: "2013-10-25" "2013-10-25" "2013-10-26" "2013-10-26" ...
 $ TIME  :Formal class 'Period' [package "lubridate"] with 6 slots
  .. ..@ .Data : num  0 0 0 0 0 0 0 0 0 0 ...
  .. ..@ year  : num  0 0 0 0 0 0 0 0 0 0 ...
  .. ..@ month : num  0 0 0 0 0 0 0 0 0 0 ...
  .. ..@ day   : num  0 0 0 0 0 0 0 0 0 0 ...
  .. ..@ hour  : num  8 18 5 18 5 18 5 10 18 5 ...
  .. ..@ minute: num  0 15 17 0 10 0 0 43 0 0 ...

is this type is correct or should i get only time (like how i got type Date for DATE)??

format is (18H 15M 0S). i want to get this to the format of 18:15 or 18:15:00

Just create a reprex and then it is easier for us to help you :slight_smile: (See here: FAQ: How to do a minimal reproducible example ( reprex ) for beginners)