I want to create a sequence of times 00:00 - 12:00 and 12:00 to 00:00 with 10 minutes step.
How can I do this in R?
I have tried with:
library(chron)
t <- merge(0:23, seq(0, 50, by = 10))
chron(time = paste(x$x, ':', x$y), format = c(times = "h:m"))
But I have 2 problems:
I get an error running chron(time = paste(x$x, ':', x$y), format = c(times = "h:m")):
Error in convert.times(times., fmt) : format h:m may be incorrect
How can I turn it to standard time with AM/PM? Should I merge it twice:
t <- merge(0:12, seq(0, 50, by = 10))
t_am <- merge(t, "AM")
t_pm <- merge(t, "PM")
Or maybe another way using POSIXt?
We can use seq :
format(seq(as.POSIXct('00:00', format = "%H:%M", tz = "UTC"),
as.POSIXct(Sys.Date() + 1), by = '10 mins'), "%I:%M%p")
#[1] "12:00AM" "12:10AM" "12:20AM" "12:30AM" "12:40AM" "12:50AM" "01:00AM ...
#[141] "11:20PM" "11:30PM" "11:40PM" "11:50PM" "12:00AM"
Make sure you have the correct locale or set it via :
Sys.setlocale("LC_TIME", "en_US.UTF-8")!
Related
My initial data is in %y-%m-%d format...
using the code
returnsgamma <- read.zoo(returns, header = TRUE, sep = ",", FUN = as.chron)
the zoo file is returning values in the order %m/%d/%y
is there anyway to read.zoo and have the order of dates stay as %y/%m/%d or %d/%m/%y?
Assuming the input shown in the Note at the end we can use the default Date class whose output when rendering defaults to yyyy-mm-dd or use chron with chron(..., out.format="y-m-d") which produces yy-mm-dd.
library(zoo)
read.csv.zoo(text = Lines, format = "%y-%m-%d")
## 2022-12-01
## 34
library(chron)
toChron <- function(x) as.chron(x, out.format = "y-m-d")
read.csv.zoo(text = Lines, FUN = toChron)
## 22-12-01
## 34
Note
Lines <- "date,value
22-12-01,34"
if I print to console
AirPassengers
I is nicely formated with header and row names, why following code is not also siniliary formated?
as.ts(
read.zoo(
data.frame(
date = seq(Sys.Date()-365, Sys.Date(), by = "day"),
value = seq(1, 366)
)
)
)
When you use the read.zoo function, it resturns a zoo object, which is a type of character.
If you get rid of the function you get:
as.ts(
data.frame(
date = seq(Sys.Date()-365, Sys.Date(), by = "day"),
value = seq(1, 366)
)
)
You should get a time series object that is a matrix format
If you must use read.zoo
ts <- as.ts(
zoo::read.zoo(df)
) |> data.frame() |> setNames(names(df[2]))
)
I am trying to get the date as Jan 20 to June 20 in the month column of rpivottable. but it's always showing in yyyy-mm-dd format e.g. 2020-01-01. My code as below:
library(readxl)
library(rpivotTable)
myexcel <- read_excel("claimH1data_date.xlsx")
x <- myexcel$Month
as.Date(x, format, tryFormats = c("%m-%Y"),tz = "UTC",
optional = TRUE)
format(x, format="%B %Y")
View(x)
rpivotTable(myexcel, rows = "Month",cols="Action", vals = "Freq",
aggregatorName = "Count", rendererName = "Table")
Can you please help? Thanks.
You can try :
library(readxl)
#Read the data
myexcel <- read_excel("claimH1data_date.xlsx")
#Sort the data based on date
myexcel <- myexcel[order(myexcel$Month), ]
#Apply the format
myexcel$Month <- format(myexcel$Month, format="%B %Y")
myexcel
I have the following code:
gsub("-","/",paste(cut(seq(as.POSIXct(Sys.Date(),format="%d-%b-%y"), by = "-1 day", length.out = 10),"days"),collapse = ","))
The output:
"2019/03/20,2019/03/19,2019/03/18,2019/03/17,2019/03/16,2019/03/15,2019/03/14,2019/03/13,2019/03/12,2019/03/11"
However the desired result is
'20/03/2019','19/03/2019','18/03/2019','17/03/2019','16/03/2019','15/03/2019','14/03/2019','13/03/2019','12/03/2019','11/03/2019'
How can I accomplish that ?
Regards
Not sure what you are trying to do but you can generate the required output by doing
format(Sys.Date() - 1:10, "%d/%m/%Y")
#[1] "20/03/2019" "19/03/2019" "18/03/2019" "17/03/2019" "16/03/2019" "15/03/2019"
# "14/03/2019" "13/03/2019" "12/03/2019" "11/03/2019"
I'm looking for a way to convert decimal hours to HH:MM:SS
For instance, as input:
4.927778 hours
Desired output:
04:55:40
You can try something like below
dh <- 4.927778
strftime(as.POSIXct(dh * 60 * 60, origin = Sys.Date(), tz = "GMT"), format = "%H:%M:%S")
## [1] "04:55:40"
You should be able to get an idea of what you need to do -
a <- "4.927778 hours"
a <- as.numeric(gsub(x = a,pattern = " hours", replacement = ""))
h <- a%/% 1
m <- ((a%% 1)*60) %/% 1
s <- round((((a%% 1)*60) %% 1)*60,0)
paste(h,m,s,sep = ":")
#[1] "4:55:40"
An alternative solution is to convert this to a date/time class and then format it in an appropriate way.
format(ISOdatetime(1900,1,1,0,0,0, tz="GMT") +
as.difftime(4.927778, unit="hours"), "%H:%M:%S")
You can use sprintf() to format the output as you wish when you have the number of hours, minutes and seconds as integers. These can be calculated using modulo (%%) and floor()/round(). The number of hours can be extracted from a string very nicely using the parse_number() function from the readr package:
library(readr)
input <- "4.927778 hours"
hrs <- parse_number(input)
hours <- floor(hrs)
minutes <- floor((hrs %% 1) * 60)
seconds <- round((((hrs %% 1) * 60) %% 1) * 60)
sprintf("%02d:%02d:%02d", hours, minutes, seconds)
The advantage of this strategy is that it still works for time differences larger than 24 hours in contrast to the solutions using strftime().
This should work with negative values as well.
convertHours<-function(hours){
timeoffset<-as.numeric(as.POSIXct(format(Sys.time(), tz = "GMT")) - as.POSIXct(format(Sys.time(), tz = "")))
hoursMinutes<-ifelse(hours<0,paste0("-",strftime(as.POSIXct((abs(hours)+timeoffset) * 60 * 60, origin = Sys.Date(), tz =""), format = "%H:%M")), strftime(as.POSIXct((hours+timeoffset) * 60 * 60, origin = Sys.Date(), tz =""), format = "%H:%M"))
}
h<-1.33
hhmm<-convertHours(h)
hhmm [1] "01:19"
h<--1.33
hhmm<-convertHours(h)
hhmm [1] "-01:19"
If you are using .net/C# you can use a little bit of date math.
var when = DateTime.UtcNow; // or any time of your choice
var later = when.AddHours(4.927778);
var span = later - when;
Console.WriteLine(span)
I see the R flag now. Perhaps this will give you a hint where to look for something similar. I don't know R.