Question: Separating Time from Date and changing the format of time column in RStudio
0
gravatar for paramount.amin
16 days ago by
paramount.amin0 wrote:

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 • 119 views
ADD COMMENTlink modified 15 days ago • written 16 days ago by paramount.amin0

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

ADD REPLYlink written 16 days ago by rpolicastro2.0k

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

ADD REPLYlink written 15 days ago by paramount.amin0
1
gravatar for rpolicastro
16 days ago by
rpolicastro2.0k
rpolicastro2.0k wrote:

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 COMMENTlink written 16 days ago by rpolicastro2.0k

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 REPLYlink modified 15 days ago • written 15 days ago by paramount.amin0

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

ADD REPLYlink written 15 days ago by rpolicastro2.0k

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

ADD REPLYlink written 15 days ago by paramount.amin0

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

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

ADD REPLYlink written 15 days ago by paramount.amin0
Please log in to add an answer.

Help
Access

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
Powered by Biostar version 2.3.0
Traffic: 2024 users visited in the last hour