Separating Time from Date and changing the format of time column in RStudio
1
0
Entering edit mode
4.1 years ago

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 • 999 views
ADD COMMENT
0
Entering edit mode

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

ADD REPLY
0
Entering edit mode

I also tried to use this code, but it produces NAs

ADD REPLY
1
Entering edit mode
4.1 years ago

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
ADD COMMENT
0
Entering edit mode

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)

ADD REPLY
0
Entering edit mode

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

ADD REPLY
0
Entering edit mode

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

ADD REPLY
0
Entering edit mode

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

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

ADD REPLY

Login before adding your answer.

Traffic: 2401 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6