R code to change a column from datetime to time only - r

I am working with R, and have a column in datetime (2021-05-31 13:25:10) and I want to change it to time only (HH:MM:SS) by removing the date.
Thanks.

We can use as.ITime from data.table
library(data.table)
df1$time <- as.ITime(df1$datetime)
If we need to get the hours only
library(dplyr)
library(lubridate)
df1 <- df1 %>%
mutate(datetime = ymd_hms(datetime),
hours = hour(datetime), mins = minute(datetime),
secs = second(datetime))
data
df1 <- data.frame(datetime = "2021-05-31 13:25:10")

You can use format to extract time component from date-time.
df <- data.frame(datetime = Sys.time() + sample(10))
df$time <- format(df$time, '%T')

Related

Converting ddmmyy-xxxx to date in R

I have a dateframe with a column with numbers that represent a date. So 110190-1111 is ddmmyy-xxxx, where the x's don't matter. It is implicit that the century is 1900.
df <- c("110190-1111", "220391-1111", "241287-1111")
I would like to have it converted to.
c("1990-01-11", "1991-03-22", "1987-12-24)
I have removed the last 4 digits and the "-" with the following.
ID <- c("110190-1111", "220391-1111", "241287-1111")
df <- data.frame(ID)
df <- df %>% mutate(date=gsub("-.*", "", ID))
I have tried fiddling with the as.Date function with no luck. Any suggestions? Thanks.
as.Date ignores junk at the end so
df %>% mutate(Date = as.Date(ID, "%d%m%y"))
giving:
ID Date
1 110190-1111 1990-01-11
2 220391-1111 1991-03-22
3 241287-1111 1987-12-24
or using only base R:
transform(df, Date = as.Date(ID, "%d%m%y"))
We can use dmy from lubridate
library(lubridate)
df$date <- dmy(df$date)

Convert character to date and numeric, maintain same format

In the output of the code below the variables day and sales are in the format that I need but not the type, it outputs type chr instead. The variables should be date and num respectively. I've tried many things but either I get chr or some sort of error. For instance, using as.Date() doesn´t change the variable day to the format "%d/%m/%Y". The code with sample data:
library(dplyr)
library(lubridate)
df <- data.frame(matrix(c("2017-09-04","2017-09-05",103,104,17356,18022),ncol = 3, nrow = 2))
colnames(df) <- c("DATE","ORDER_ID","SALES")
df$DATE <- as.Date(df$DATE, format = "%Y-%m-%d")
df$SALES <- as.numeric(as.character(df$SALES))
df$ORDER_ID <- as.numeric(as.character(df$ORDER_ID))
TOTALSALES <- df %>%
select(ORDER_ID,DATE,SALES) %>%
mutate(weekday = wday(DATE, label=TRUE)) %>%
mutate(DATE=as.Date(DATE)) %>%
filter(!wday(DATE) %in% c(1, 7) & !(DATE %in% as.Date(c('2017-01-02','2017-02-27','2017-02-28','2017-04-14'))) ) %>%
group_by(day=floor_date(DATE,"day")) %>%
summarise(sales=sum(SALES)) %>%
data.frame()
TOTALSALES$day <- TOTALSALES$day %>%
as.POSIXlt(, tz="America/Sao_Paulo") %>%
format("%d/%m/%Y")
TOTALSALES$sales <- TOTALSALES$sales %>%
format(digits=9, decimal.mark=",",nsmall=2,big.mark = ".")
TOTALSALES$day <- as.Date(df$DATE, format = "%d/%m/%Y")
Any idea how can I solve this problem or a direction on how it should be done ?
Appreciate any help
I'm not sure I understand your question.
To print a Date object in a particular date-time format you can use format
# This *converts* a character vector/factor to a vector of Dates
df$DATE <- as.Date(df$DATE, format = "%Y-%m-%d")
# This *prints* the Date vector as a character vector with format "%d/%m/%Y"
format(df$DATE, format = "%d/%m/%Y")
Minimal example
ss <- c("2017-09-04","2017-09-05")
date <- as.Date(ss, format = "%Y-%m-%d")
format(date, format = "%d/%m/%Y")
#[1] "04/09/2017" "05/09/2017"

Want to combine date and time in a column using R

I have the following dataframe
Date Time
10/03/2014 12.00.00
11/03/2014 13.00.00
12/03/2014 14.00.00
I want to create one single column as follows
DT
10/03/2014 12.00.00
11/03/2014 13.00.00
12/03/2014 14.00.00
when I run
data$DT <- as.POSIXct(paste(x$Date, x$Time), format="%d-%m-%Y %H:%M:%S")
I get a column DT with all NA values.
Data$DT <- as.POSIXct(as.character(paste(data$Date, data$Time)), format="%d/%m/%Y %H.%M.%S")
OR
data$Time <- gsub('\\.',':',data$Time)
data$Date <- gsub('/','-',data$Date)
data$DT <- as.POSIXct(as.character(paste(data$Date, data$Time)), format="%d-%m-%Y %H:%M:%S")
Use the package lubridate:
data$DT <- with(data, ymd(Date) + hms(Time))
If you want the column to be a POSIXct, do the following after that:
data$DT <- as.POSIXct(data$DT)
This should be a very common problem, hence contributing with a reproducible answer using dplyr:
## reproducible example
library(dplyr)
library(magrittr)
DF <- data.frame(Date = c("10/03/2014", "11/03/2014", "12/03/2014"),
Time = c("12.00.00", "13.00.00", "14.00.00"))
DF_DT <- DF %>%
mutate(DateTime = paste(Date, Time)) %>%
mutate(across('DateTime', ~ as.POSIXct(.x, format = "%d/%m/%Y %H.%M.%S")))

Format Date to Year-Month in R

I would like to retain my current date column in year-month format as date. It currently gets converted to chr format. I have tried as_datetime but it coerces all values to NA.
The format I am looking for is: "2017-01"
library(lubridate)
df<- data.frame(Date=c("2017-01-01","2017-01-02","2017-01-03","2017-01-04",
"2018-01-01","2018-01-02","2018-02-01","2018-03-02"),
N=c(24,10,13,12,10,10,33,45))
df$Date <- as_datetime(df$Date)
df$Date <- ymd(df$Date)
df$Date <- strftime(df$Date,format="%Y-%m")
Thanks in advance!
lubridate only handle dates, and dates have days. However, as alistaire mentions, you can floor them by month of you want work monthly:
library(tidyverse)
df_month <-
df %>%
mutate(Date = floor_date(as_date(Date), "month"))
If you e.g. want to aggregate by month, just group_by() and summarize().
df_month %>%
group_by(Date) %>%
summarize(N = sum(N)) %>%
ungroup()
#> # A tibble: 4 x 2
#> Date N
#> <date> <dbl>
#>1 2017-01-01 59
#>2 2018-01-01 20
#>3 2018-02-01 33
#>4 2018-03-01 45
You can solve this with zoo::as.yearmon() function. Follows the solution:
library(tidyquant)
library(magrittr)
library(dplyr)
df <- data.frame(Date=c("2017-01-01","2017-01-02","2017-01-03","2017-01-04",
"2018-01-01","2018-01-02","2018-02-01","2018-03-02"),
N=c(24,10,13,12,10,10,33,45))
df %<>% mutate(Date = zoo::as.yearmon(Date))
You can use cut function, and use breaks="month" to transform all your days in your dates to the first day of the month. So any date within the same month will have the same date in the new created column.
This is usefull to group all other variables in your data frame by month (essentially what you are trying to do). However cut will create a factor, but this can be converted back to a date. So you can still have the date class in your data frame.
You just can't get rid of the day in a date (because then, is not a date...). Afterwards you can create a nice format for axes or tables. For example:
true_date <-
as.POSIXlt(
c(
"2017-01-01",
"2017-01-02",
"2017-01-03",
"2017-01-04",
"2018-01-01",
"2018-01-02",
"2018-02-01",
"2018-03-02"
),
format = "%F"
)
df <-
data.frame(
Date = cut(true_date, breaks = "month"),
N = c(24, 10, 13, 12, 10, 10, 33, 45)
)
## here df$Date is a 'factor'. You could use substr to create a formated column
df$formated_date <- substr(df$Date, start = 1, stop = 7)
## and you can convert back to date class. format = "%F", is ISO 8601 standard date format
df$true_date <- strptime(x = as.character(df$Date), format = "%F")
str(df)

How can I count average temperature every hour in many different columns in R

I have a data frame which consists of date and temperature of 34 different systems each system in different column. I need to calculate every systems average hourly temperature. I use this code to calculate average for 1 system. But if I want to calculate average for other 33 systems, I have to repeat code again, and again. Is there a better way to find hourly average in all columns at once ?
dat$ut_ms <- dat$ut_ms/1000
dat[ ,1]<- as.POSIXct(dat[,1], origin="1970-01-01")
dat$ut_ms <- strptime(dat$ut_ms, "%Y-%m-%d %H:%M")
dat$ut_ms <- cut(dat[enter image description here][1]$ut_ms, breaks = 'hour')
meanNPWD2401<- aggregate(NPWD2401 ~ ut_ms, dat, mean)
I added a picture of the data. For better understing of what I want.
You can split your data per hour and itterate,
list1 <- split(dat, cut(strptime(dat$ut_ms, format = '%Y-%m-%d %H:%M'), 'hour'))
lapply(list1, colMeans)
When you rearrange the data into a long format, things get much easier
n.system <- 34
n.time <- 100
temp <- rnorm(n.time * n.system)
temp <- matrix(temp, ncol = n.system)
seconds <- runif(n.time, max = 3 * 3600)
time <- as.POSIXct(seconds, origin = "1970-01-01")
dataset <- data.frame(time, temp)
library(dplyr)
library(tidyr)
dataset %>%
gather(key = "system", value = "temperature", -time) %>%
mutate(hour = cut(time, "hour")) %>%
group_by(system, hour) %>%
summarise(average = mean(temperature))

Resources