This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Separating Time from Date and changing the format of time column in RStudio

Hello I am processing a CSV file in Rstudio that has a date column like follow for five minutes time steps;

2019-01-01 00:00:00 2019-01-01 00:05:00 2019-01-01 00:10:00 . . Time starts from the first day of January to the end of the year. and at the end of the year, this pattern will be repeated 30 times ( I am investigating the indoor temperature of 30 different homes all over the year for 5 minutes time intervals). how can I have time in a different column for my entire CSV file in Rstudio?

r

Can you post some example data using dput(head(df))?

1 answer

Example data.

df <- data.frame(time=c("2019-01-01 00:00:00", "2019-01-01 00:05:00", "2019-01-01 00:10:00"))

> df
                 time
1 2019-01-01 00:00:00
2 2019-01-01 00:05:00
3 2019-01-01 00:10:00

Separating the date and time, and then formatting them to proper data structure using lubridate.

library("tidyr")
library("lubridate")

df <- df %>%
  separate(time, into=c("date", "time"), sep=" ") %>%
  mutate(date=ymd(date), time=hms(time))

> df
        date   time
1 2019-01-01     0S
2 2019-01-01  5M 0S
3 2019-01-01 10M 0S

I am running a Neural Network analysis and the time column format needs to be changed to numeric. How can I change that? (When I try to use as.numeric(), it turns all the value to N.A)

as.numeric should work. df <- mutate(df, time=as.numeric(time)) will convert the time into the numeric value of seconds.

I received this error: In eval(cols[[col]], .data, parent.frame()) : NAs introduced by coercion

Is this because of the method that I am using for time separation?

dt$date <- sapply(strsplit(as.character(dt$DateTime), " "), "[", 1)

Log in to answer this question.