Time Conversion Only (Hour and Minute) [duplicate] - r

This question already has answers here:
keep only hour: minute:second from a "POSIXlt" "POSIXt" object
(1 answer)
how do you pick the hour, minute and second from the posixct formated datetime in R
(2 answers)
Date time conversion and extract only time
(6 answers)
Closed 3 years ago.
I have observation data which only give hour and minute mark, the data looks like this:
- date | time | value
- 2019-01-01 | 00:00 | 14
- 2019-01-01 | 00:30 | 23
- 2019-01-01 | 01:00 | 32
- 2019-01-01 | 01:30 | 41
- 2019-01-02 | 00:00 | 41
- 2019-01-02 | 00:30 | 32
- 2019-01-02 | 01:00 | 23
- 2019-01-02 | 01:30 | 14
- ....
I successfully convert the date into the data format using
data$date <- as.Date(data$date, "%Y/%m/%d")
but when i try to convert the time to its format, i encounter problem, I tried using this:
data$time <- strptime(data$time, "%H:%M")
this give me the result of time with current date: "2019-03-14 00:00:00". which is not what i'm looking for and the date is false
I also tried using:
trydata$jam <- timestamp(trydata$jam, "%H:%M")
this give me the result:
%H:%M00:00 ------##
What is the best way to do this? I also want to extract the data in certain duration of time (like from 10:00 to 13:00)
Thank You

Related

Extract AM/PM from Time in R [duplicate]

This question already has answers here:
Extract part of string before the first semicolon
(4 answers)
Create categories by comparing a numeric column with a fixed value
(3 answers)
Closed 2 years ago.
Hi I have a sample data frame like this
Time <- c('0:00', '1:00', '2:00', '13:00', '14:00')
Time = data.frame(x)
So what I would like to do is create another column "AMPM" based on the "Time" column. "AMPM" should able to show if the time is in AM or PM
The final output should look like this
Time AMPM
1 0:00 AM
2 1:01 AM
3 2:09 AM
4 13:52 PM
5 14:06 PM
6 15:33 PM
7 16:27 PM
8 21:40 PM
You can remove everything after colon, convert data to integer and assign 'PM' to all the values greater than 11 and "AM" otherwise.
df <- data.frame(Time = c('0:00', '1:00', '2:00', '13:00', '14:00'))
df$AMPM <- ifelse(as.integer(sub(':.*', '', df$Time)) > 11, 'PM', 'AM')
#Without ifelse
#c('AM', 'PM')[(as.integer(sub(':.*', '', x)) > 11) + 1]
df
# Time AMPM
#1 0:00 AM
#2 1:00 AM
#3 2:00 AM
#4 13:00 PM
#5 14:00 PM

Incorrect replacement of strings in R

I need to replace awkward strings in R, specifically the times that are in a weird format. The data looks like this:
Date | Time | AmbientTemp
2000-01-01 | 11:00 a | 25
2000-01-01 | 11:30 a | 25.5
2000-01-01 | 11:00 p | 20
2000-01-01 | 11:30 p | 19.5
The a and p mean AM and PM respectively (obviously).
lubridate and base R cannot convert these dates to a correct format. Thus, I turned to the cumbersome str_replace_all function (from package stringr) to convert ALL my times in a large dataframe: >130000 records.
Example functions:
uploadDat$Time = str_replace_all(uploadDat$Time,"11:00 a","11:00")
uploadDat$Time = str_replace_all(uploadDat$Time,"11:00 p","23:00")
I changed the class of the times using as.character() before applying stringr's functions.
The result is perfect except for the 11'o clock times (like above) that are converted as follow:
Date | Time | AmbientTemp
2000-01-01 | 101:00 | 25
2000-01-01 | 101:30 | 25.5
2000-01-01 | 113:30 | 20
2000-01-01 | 113:30 | 19.5
Why are these specific times converted incorrectly?
We can paste "m" at the end of time, convert it into POSIXct
format(as.POSIXct(paste0(df$Time, "m"), format = "%I:%M %p"), "%T")
#[1] "11:00:00" "11:30:00" "23:00:00" "23:30:00"

R- Create a new field applying condition on a Date field

I am new in R. I am working with windows 10. I have R Studio and R version 3.5.0.
I have a table with one field dateTime format.
2012-02-02 10:04:00
2012-08-13 11:38:00
2012-07-13 14:00:00
2012-09-26 08:45:00
2012-10-24 05:39:00
2012-02-03 03:33:00
2012-05-02 06:30:00
2012-06-27 09:00:00
2012-07-09 10:16:00
2012-11-22 13:13:00
I need to create a new field that splits the data between summer and winter:
From May to September would be Summer and from October to April would be Winter. Based on the result of this new field, create another one that separates the data between times of the day: morning, noon, afternoon and night for summer and the same for winter. The conditions would be:
For Summer
* Morning Summer: 5 am – 10 am
* Noon Summer: 10 am -12 pm
* Afternoon Summer: 12 pm -8 pm
* Night summer 8 pm – 5 am
For Winter
* Morning Winter: 7 am – 11 am
* Noon Winter: 11 am -12 pm
* Afternoon Winter: 12 pm -4 pm
* Night Winter 4 pm – 7 am
the result would be something like this:
date | season | time Of Day
'2012-02-02 10:04:00' | winter | morning
'2012-08-13 11:38:00' | summer | noon
'2012-07-13 14:00:00' | summer | afternoon
'2012-09-26 08:45:00' | summer | morning
'2012-10-24 05:39:00' | winter | night
'2012-02-03 03:33:00' | winter | night
'2012-05-02 06:30:00' | summer | morning
'2012-12-27 09:00:00' | winter | morning
'2012-07-09 10:16:00' | summer | morning
'2012-11-22 13:13:00' | winter | afternoon
For the first case, (split between summer and winter) I tried to use case_when, but it did not work:
df %>%
mutate(season = case_when(
month(.$date) > 4 & month(.$date)< 10 ~ "summer",
month(.$date) < 5 & month(.$date) > 10 ~ "winter"
))
Error in mutate_impl(.data, dots) :
Evaluation error: do not know how to convert 'x' to class
<U+0093>POSIXlt<U+0094>.
I tried to find something about the error, but to be honest I did not get how to solve the problem. I tried to use library "lubridate" but still doesn't work.
Any idea of how to do it?
df %>% mutate_if(is.character, as.POSIXct) %>%
mutate(season = case_when(
month(date) > 4 & month(date) < 10 ~ "summer",
month(date) < 5 & month(date) > 10 ~ "winter"
))
Data
data <- read.table(text="
date
'2012-02-02 10:04:00'
'2012-08-13 11:38:00'
'2012-07-13 14:00:00'
'2012-09-26 08:45:00'
",header=T, stringsAsFactors = F)

why datetime showed not like in the DB?

I've in the database the following lines
id | date_order | name | origin
----+---------------------+----------+---------
38 | 2016-05-10 14:00:00 | OT/00024 | GI/00005:
39 | 2016-05-26 14:00:00 | OT/00025 | GI/00005:
40 | 2016-06-11 14:00:00 | OT/00026 | GI/00005:
41 | 2016-06-27 14:00:00 | OT/00027 | GI/00005:
42 | 2016-07-13 14:00:00 | OT/00028 | GI/00005:
but it showed in the views as:
date_order | name | origin
--------------------+----------+-------------
10/05/2016 15:00:00 | OT/00024 | GI/00005:
26/05/2016 15:00:00 | OT/00025 | GI/00005:
11/06/2016 14:00:00 | OT/00026 | GI/00005:
27/06/2016 14:00:00 | OT/00027 | GI/00005:
13/07/2016 15:00:00 | OT/00028 | GI/00005:
I changed Timezone but I still get the difference !
When you store the datetime, you should use context like this:
from openerp.osv import fields
from datetime import datetime
...
my_date = fields.datetime.context_timestamp(cr, uid, datetime.now(), context=context)
The date stored in the database is UTC (GMT-0) timezone. Assume that the person is set with timezone GMT - 5:00, then while storing the value to the database, the date will be added with 5 hrs (exactly 5, not little more or little less) and thus we get the UTC time to store into the database. Now when displaying the same it will check for the users timezone and it finds that its GMT - 5:00 so the database time will be subtracted with 5 (again exactly 5, not little more or little less) and displayed the user.
This will be great for system which is used in different timezones. So the understanding is the input is taken in the user's timezone stored in UTC(GMT-0) and displayed to user's timezone (even if the user viewing is in the different timezone the time will be accurate to their timezone)
Odoo display the datetime field AS TIMEZONE, in this case the timezone is GMT+1 but it will be GMT+0 in june because of ramadan, that's why

Cognos 10 - Display data from an SQL query on multiple rows in a list

I have a query that returns a) worked hours and b) non-worked hours for the same work/task to be displayed for each day of the week in different rows.
List block display should look like:
Header: Work/Task| Worked/Non-Worked Hours| Day 1| Day 2 ....Day7
Data row1: Work/Task | Worked Hours (just text label)| Day 1 hrs| Day 2 hrs.... Day 7 hrs
Data row2: Work/Task | Non-Worked Hours (just text label) | Day 1 hrs| Day 2 hrs.... Day 7 hrs
I've got the report display created as above, and the query returns the data correctly, but the problem is when there are multiple work/tasks for the same week, the list only seems to be displaying the non-worked hours for the last task returned.
For example: Let's say there are 3 tasks for the day: A,B, C
The display looks like:
Header: Work/Task| Worked/Non-Worked Hours| Day 1| Day 2 ....Day7
Data row1: A | Worked Hours| Day 1 hrs| Day 2 hrs.... Day 7 hrs
Data row2: B | Worked Hours| Day 1 hrs| Day 2 hrs.... Day 7 hrs
Data row3: C | Worked Hours| Day 1 hrs| Day 2 hrs.... Day 7 hrs
Data row4: C | Non-Worked Hours| Day 1 hrs| Day 2 hrs.... Day 7 hrs
Ideally, it should display non-worked hours for A and B too. Like I said, the query returns these values, but the list doesn't seem to display it.
Expected result:
Header: Work/Task| Worked/Non-Worked Hours| Day 1| Day 2 ....Day7
Data row1: A | Worked Hours| Day 1 hrs| Day 2 hrs.... Day 7 hrs
Data row2: A | Non-Worked Hours| Day 1 hrs| Day 2 hrs.... Day 7 hrs
Data row3: B | Worked Hours| Day 1 hrs| Day 2 hrs.... Day 7 hrs
Data row4: B | Non-Worked Hours| Day 1 hrs| Day 2 hrs.... Day 7 hrs
Data row5: C | Worked Hours| Day 1 hrs| Day 2 hrs.... Day 7 hrs
Data row6: C | Non-Worked Hours| Day 1 hrs| Day 2 hrs.... Day 7 hrs
When there is only only task involved though, the list displays both worked and non-worked hours as excepted. Any thoughts on what I could be doing wrong here?
On your final query that is feeding into your list, change the Aggregate Function from 'Automatic' to 'None' for your first two columns, and set the remaining columns to have Aggregate Function of 'Total'.
It sounds like the automatic aggregation is doing a max or min. When you are working with queries that pull from other queries in Report Studio, make sure you are using your 'Run -> View Tabular Data' on each individual query, so you know at what step the data is being lost.
Thanks for the tips, guys. The query was built with several UNIONS. I managed to identify and rectify the issue myself. I selected the complete list and applied a GROUPING based on Non-Worked Hours. That did the trick.

Resources