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)
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?
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
as.numeric should work. df <- mutate(df, time=as.numeric(time)) will convert the time into the numeric value of seconds.
Log in to answer this question.
Can you post some example data using
dput(head(df))?I also tried to use this code, but it produces NAs