Convert factor into specific time zone date - r

I am trying to convert a factor into date format for specific region.
My table looks like this
Period State
2019-03-01T04:05:00+10:0 NSW
2019-03-01T04:05:00+10:00 QLD
I am trying to convert the period into a dateformat for the specific region as time zone for NSW and QLD will be different.
Already tried the lubridate package:
as.Date(df_total$Period, format = "%YYYY-%mm-%DD")
Iam getting NA while what I exactly need is the correct time for New South Wales and Queensland. Not sure if it can pick up the +10 and also change that to date at the same time.

Related

Not able to round date to nearest hour in R tried lubridate, strptime, and POSIXCT

Following is an example of the column of the data frame
time
00:01:47
01:05:45
17:10:00
20,551,697 more rows
The data type is character
The data is from Citi Bikes New York for the period January 2019 to December 2019
Note: the data that starts with JC is for New Jersey not New York City
Since the data is very large you can download just one months data to try the code:
The column time is not a part of the original data frame and was created by me using the following code:
ridedata_clean$time <- format(as.POSIXct(ridedata_clean$starttime),format = "%H:%M:%S")
Then I tried to change the format from chr to time or date time, using the follwoing code:
ridedata_clean$time <- format(ridedata_clean$time, format ="%H:%M")
But glimpse() still shows the data type as so I used strptime(), but it still did'nt change the format
ridedata_clean$time <- strptime(ridedata_clean$time, "%H:%M:%S")
I have tried to use the round function, but it didn't work:
round(ridedata_clean$time, units = "hours")
The I tried lubridate, but it didn't work, the code below maybe wrong since I am typing from memory as I was unbale to save this change:
round_date(ridedata_clean$time, "%H,%M,%S")
I am new to data analysis and this is my second project, please help.

R, intraday data, convert datetime to a different timezone

I have a dataset with intraday data where three important variables are Country, Datetime, Price.
An example could be:
Sweden, 2019-12-23 09:08:00, 105.31
This data is downloaded from Bloomberg, and it looks like it uses my local time (Denmark). For example, for Australia I have a market which starts at 23 00 which does not make sense unless it is European time. I would like to convert the time that I have in the data to the local time in that particular country. Of course, I could add or subtract some hours, but the time difference is not fixed: some countries have summer/winter time while other countries don't, and countries which do have summer/winter time may change on different days (for example I think there is about one week between the time change in US and Europe). Do you have an advice how to transform my dataset into the local timezone? So, if it says "2019-12-23 09:08:00", then I would like to know that in that particular country it was 09:08 in the morning (and not in my country). I really hope there is a smart R function for this.
Thanks in advance!
You could use lubridate::force_tz and lubridate::with_tz:
dat <- as.POSIXct("2021-05-01 12:00:00",tz = "UTC")
lubridate::force_tz(dat,tz="CET")
#> [1] "2021-05-01 12:00:00 CEST"
lubridate::with_tz(lubridate::force_tz(dat,tz="UTC"))
#> [1] "2021-05-01 14:00:00 CEST"

For looping over dates

So I have a dataframe called Swine_flu_cases that looks as follows (just an extract):
Country Date Confirmed
1 Canada 2020-01-22 1
2 Egypt 2020-01-23 1
3 Algeria 2020-01-24 1
4 France 2020-01-25 1
5 Zambia 2020-01-26 1
6 Congo 2020-01-27 1
This data set looks at the recorded amount of swine flu cases of a country on a specific date.
I have filtered my data to only show variables where the confirmed cases are 1 and have also grouped it by the different country and sorted it by ascending order of date. (I did this to get the dates that these countries each had their first cases)
I have sorted it in ascending order of date because I want to extract the first time each country had their first recorded swine flu case and store that as a vector.
I have tried doing so by using the following code :
first_case_date = as.Date(data.frame(Swine_flu_cases$Date))
This however gave me an error though.
Error in as.Date.default(data.frame(Swine_flu_cases$Date)) : do
not know how to convert 'data.frame(Swine_flu_cases$Date)' to class
“Date”
What I want to do is create a new variable Swine_flu_cases$days_since_first_case which will take the stored date of each of the countries on my lists first case and subtract that from all the other dates for each country.
My knowledge of for loops is very basic but I know I need to somehow use a for loop for this. I have recently familiarised myself with the lead and lag function as well and was thinking maybe there is a way in which I could combine these two functions to create this variable?
If someone can just give me a general idea on how I could go about doing this please I would really appreciate it.
You can do this with dplyr and lubridate to make your dates behave.
library(dplyr)
library(lubridate)
Swine_flu_cases %>%
mutate(Date = ymd(Date) %>% # makes the Dates behave better for subtraction
group_by(Country) %>% # You want grouped by country
mutate(days_since_first_case = Date - min(Date))
# subtracts the first date in each group from the current date for the row)

Convert from dd/mm/yyyy to dd/mm in r

I have data spread over a period of two months. When I graph data points for each day, dates (dd/mm/yyyy) are overlapping and it is not possible to make sense of which date a certain point refers to. I tried to remove years from the date as they are not useful for the info I have and the dd/mm should leave enough space.
df$date<-as.Date(df$date, format="%d/%m")
However, it transforms the 01/09/2014 to 2015-09-01. I read that when the year is missing as.Date assumes current year and inputs it. Can I avoid this automatic insertion somehow?
something like this?
date <- as.Date("01/09/2014", format = %d/%m/%Y)
format(date, "%d/%m")
"01/09"

Create polar plots but missing data

We have these data below in a csv file and we would like to create a polar plot from them. We are going to use this R package - openair for creating the polar plot.
timestamp humandate NOppb
1 1412877113 09/10/2014 13:51 19
2 1412876508 09/10/2014 13:41
3 1412876508 09/10/2014 13:41
4 1412877118 09/10/2014 13:51 17
....
However, we are missing some data for using polarPlot(),
# Load package.
library("openair")
polarPlot(dat, pollutant = "NOppb", na.rm = TRUE)
result:
Can't find the variable(s) wd ws
Error in checkPrep(mydata, vars, type, remove.calm = FALSE) :
It requires columns of wd and ws for wind direction and speed which we don't have.
I am told that we can pull these missing data from wunderground's api, but the problem are:
how can pull the data from wunderground's api to match each row of our data above?
the weather data is measured and recorded hourly as it seems but our data is not recorded hourly as you can see it above. so how is this going to match?
Any ideas what can I do?
The openair package provides easy access to UK air quality monitoring station data, including several stations in London. These data will automatically include wind speed and direction (ws and wd). This capability is provided by openair's importAURN and importKCL functions.
Use one of these functions to download an hourly dataset from a monitoring station near your site, for the period of time you are interested in, and merge it with your data by date (timestamp). Timestamps (date column) in openair is a POSIXct date, usually to a whole hour. You will need to convert your timestamp or humandate to POSIXct using as.POSIXct, and name the resulting column date. Then round your date to the nearest whole hour, before merging on date with the AURN dataset.
Then you can make your polar plots based on the wind data, and even compare pollutant measurements to the city's own monitoring station data.
I don't know anything about specific stations in London, but read about this under importAURN and importKCL functions in the openair manual, or help in R after openair is loaded. See openair on CRAN, or the lastest updates on github (https://github.com/davidcarslaw/openair). ps: The openair author is an expert on London air quality.

Resources