Below is what my data looks like. My data is called sales1156.
> sales1156
date.and.time hsales
06/01/11 09:00 14.00
06/01/11 10:00 28.00
06/01/11 11:00 28.00
06/01/11 12:00 28.00
06/01/11 13:00 28.00
06/01/11 14:00 28.00
The data continues till 4th Oct 2013(04/10/2013). I have used the following commands to create a time series object.
> hsales1156xts<-as.xts(sales1156,order.by=as.Date(sales1156$date.and.time,frequency=24))
> is.xts(hsales1156xts)
[1] TRUE
The problem is that I am not able to plot a proper graph.
> plot.xts(hsales1156xts) # This command is throwing a warning as mentioned below
Warning message:
In plot.xts(hsales1156xts) : only the univariate series will be plotted
Stackoverflow is not allowing me to attach the graph. Someone please help me to plot this time series. Any good read or suggestion would be great. I am unable to make much out of the xts and zoo documents. Thus a little detailed syntax and explanation is required.
The date column needs to be excluded from data input in as.xts(x=
Test Example:
require(PerformanceAnalytics)
data(economics)
colnames(economics)
#[1] "date" "pce" "pop" "psavert" "uempmed" "unemploy"
#Subset your timeseries
economics_sub=economics[,c("date","uempmed")]
#Ensure your date or datetime object is in the correct format
economics_sub$date=as.Date(economics_sub[,1],format="%Y-%m-%d")
#Exclude date column whie reading data in "x ="
economics_xts<-as.xts(x=economics_sub[,"uempmed"],order.by=economics_sub[,"date"])
colnames(economics_xts)=colnames(economics_sub)[-1]
head(economics_xts)
# uempmed
#1967-06-30 4.5
#1967-07-31 4.7
#1967-08-31 4.6
#1967-09-30 4.9
#1967-10-31 4.7
#1967-11-30 4.8
#Plot Series using PerformanceAnalytics function 'chart_Series'
chart_Series(economics_xts)
Your Example:
#Data input
sales1156=read.csv(text='date.time,hsales
"06/01/11 09:00",14.00
"06/01/11 10:00",28.00
"06/01/11 11:00",28.00
"06/01/11 12:00",28.00
"06/01/11 13:00",28.00
"06/01/11 14:00",28.00',header=TRUE)
#Check format of your datetime index
str(sales1156)
#'data.frame': 6 obs. of 2 variables:
# $ date.time: Factor w/ 6 levels " 06/01/11 09:00",..: 1 2 3 4 5 6
# $ hsales : num 14 28 28 28 28 28
#The datetime index has been read as a factor and not as datetime object
#Convert datetime to appropriate format, in this case POSIXct format
sales1156$date.time=as.POSIXct(sales1156$date.time,format="%d/%m/%y %H:%M")
#Check if your formatting has worked as intended
str(sales1156)
#'data.frame': 6 obs. of 2 variables:
# $ date.time: POSIXct, format: "2011-01-06 09:00:00" "2011-01-06 10:00:00" ...
# $ hsales : num 14 28 28 28 28 28
#Converion to xts,exclude date column whie reading data in "x ="
hsales1156xts<-as.xts(x=sales1156[,"hsales"],order.by=sales1156[,"date.time"])
colnames(hsales1156xts)=colnames(sales1156)[-1]
head(hsales1156xts)
# hsales
#2011-01-06 09:00:00 14
#2011-01-06 10:00:00 28
#2011-01-06 11:00:00 28
#2011-01-06 12:00:00 28
#2011-01-06 13:00:00 28
#2011-01-06 14:00:00 28
#Plot Series using PerformanceAnalytics function 'chart_Series'
chart_Series(hsales1156xts)
Related
I have one table with two columns DATE and Q.
DATE Q
--------------------
2013-01-04 932
2013-01-05 409
2013-01-08 511
2013-01-11 121
2013-01-12 252
2013-01-13 201
2013-01-14 40
2013-01-15 66
2013-01-17 NA
2013-01-18 123
Classes ‘tbl_df’, ‘tbl’ and 'data.frame': 10 obs. of 2 variables:
$ DATE: POSIXct, format: "2013-01-04" "2013-01-05" "2013-01-08" "2013-01-11" ...
$ Q: num 932 409 511 121 252 201 40 66 NA 123 ..
You can see from data, there is a irregular frequency.First column have data which are converted into date format and in the second column data is numeric. So my intention is to convert this table into times series object, for further projections with forecast package.
So can anyone help me with some code to convert this table into ts object?
time <- seq(as.Date("2018-1-1"),as.Date("2019-1-1"),by=1)
df <- data.frame(Time=Time)
output <- dplyr::left_join(df,YOUR_TABLE,by="DATE")
Your table should have date column by name "DATE". So now you have NA values when your data is missing and you can transform your data to time series. I dont know if it this would help, for me sometimes it does. Maybe tackle NA problem with some replacing method.
I have a dataframe DF in which I have numerous of columns, one is with Dates and an other is the Hour.
My point is that I need to find the PRICE (dame datafra 36 hours before. All my days don't have 24 hours so I can't just shift my data set.
My idea was to look for the day before in my dataset & 12 hours before.
This is what I wrote but this is not working:
for (i in 38:nrow(DF)){
RefDay=as.Date(DF$Date[i])
HourRef=DF$Hour[i]
DF$P24[i]=DF[which(DF$Date == (RefDay-1))& which(DF$Hour == (HourRef-36)),"PRICE"]
}
Here is my DF:
'data.frame': 20895 obs. of 45 variables:
$ Hour : Factor w/ 24 levels "0","1","2","3",..: 1 2 3 4 5 6 7 8 9 10 ...
$ Date : POSIXct, format: "2016-07-01" "2016-07-01" "2016-07-01" "2016-07-01" ...
$ PRICE : num 29.4 24.7 23.4 21.9 20.2 ...
Here is a sample of my data:
DF.Hour DF.Date DF.PRICE
1 0 2016-07-01 29.36
2 1 2016-07-01 24.69
3 2 2016-07-01 23.42
4 3 2016-07-01 21.91
5 4 2016-07-01 20.19
6 5 2016-07-01 22.44
Try to fill the data.frame with full days. You can do it with complete in tidyr. It will fill the not existing values with NA.
If you have any NAs in your full data.frame you can go for the 36th element before with for example lag(price, 36).
DF <- complete(DF, Hour, Date) %>% arrange(Date)
DF$Price[is.na(DF$Price)] <- lag(Price, 36)
I'm trying to convert a column of character to time. The column has observations in only minutes and seconds.
The dataset I try it on is this:
data <- data.frame(
time = c("1:40", "1:55", "3:00", "2:16"),
stringsAsFactors = FALSE
)
data
time
1 1:40
2 1:55
3 3:00
4 2:16
I have checked other questions about converting character to time on here, but haven't found a solution to my problem.
The output I want is this:
time
1 00:01:40
2 00:01:55
3 00:03:00
4 00:02:16
strptime + as.character formatting will give you the expected result. But realise that it is a character value.
data$time <- as.character(strptime(data$time, "%M:%S"), "%H:%M:%S")
data
time
1 00:01:40
2 00:01:55
3 00:03:00
4 00:02:16
Convert to chron times class and if you want a character column format it.
library(chron)
transform(data, time = times(paste0("00:", time)))
transform(data, time = format(times(paste0("00:", time))))
hms is a time class that happens to have the print method you're asking for:
data <- data.frame(
time = c("1:40", "1:55", "3:00", "2:16"),
stringsAsFactors = FALSE
)
data$time <- hms::as.hms(paste0('00:', data$time))
data
#> time
#> 1 00:01:40
#> 2 00:01:55
#> 3 00:03:00
#> 4 00:02:16
str(data)
#> 'data.frame': 4 obs. of 1 variable:
#> $ time: 'hms' num 00:01:40 00:01:55 00:03:00 00:02:16
#> ..- attr(*, "units")= chr "secs"
You can convert to hms in other ways, if you like, e.g. by parsing with as.POSIXct or strptime and then coercing with as.hms.
I have a very big dataframe in R, containing weather data with the following format.
valid temp
1 17/08/2014 00:20 14
2 17/08/2014 00:50 14
3 17/08/2014 01:20 13.5
4 17/08/2014 01:50 13
5 17/08/2014 02:20 12
6 17/08/2014 02:50 10
I would like to convert these sub-hourly data to hourly, like the following.
valid tmpc
1 2014-08-17 00:00:00 14
2 2014-08-17 01:00:00 13.75
3 2014-08-17 02:00:00 12.5
The class of df$valid is 'factor'. I have tried first converting them to Date through POSIXct, but it gives only NA values. I have also tried changing the system locale and still I get NAs.
We can do this in base R by converting to POSIXlt, set the minute to 0, convert it back to POSIXct and aggregate to get the mean of 'temp'
df1$valid <- strptime(df1$valid, "%d/%m/%Y %H:%M")
df1$valid$min <- 0
df1$valid <- as.POSIXct(df1$valid)
aggregate(temp~valid, df1, FUN = mean)
Option 1: The lubridate solution using ceiling_date or round_date. It's not clear according to your data frame and results if what you want is to round or ceiling. For instance, in the first row you are rounding and in the third using ceiling. Anyways here the example:
library(lubridate)
df <- data.frame(i = 1, valid= "17/08/2014 01:28", temp = 14)
df$valid <- dmy_hm(df$valid)
df$valid_round <- ceiling_date(df$valid , unit="hours")
Results:
i valid temp valid_round
1 1 2014-08-17 01:28:00 14 2014-08-17 02:00:00
Option 2: using the base functions. Use:
df$valid <- as.POSIXct(strptime(df$valid, "%d/%m/%Y %H:%M", tz ="UTC"))
and then round it.
I want to retrieve the third Wedndesday of specific months in R.
This is not exactly a duplicate question of How to figure third Friday of a month in R because I want to use either Base R or XTS.
The data is in x:
library(xts)
x = xts(1:100, Sys.Date()+1:100)
and I can retrieve wednesdays by using:
wed=x[.indexwday(x) %in% 3]
> wed
[,1]
2015-09-30 6
2015-10-07 13
2015-10-14 20
2015-10-21 27
2015-10-28 34
2015-11-04 41
2015-11-11 48
2015-11-18 55
2015-11-25 62
2015-12-02 69
2015-12-09 76
2015-12-16 83
2015-12-23 90
2015-12-30 97
>
I haven't figured out how to get the third observation in each month of this wed vector using xts but there must be a way.
third=wed[head(endpoints(wed, "months") + 3, -3)]
returns a wrong result.
I have read the xts documentation and couln't find the right function there.
Any help would be appreciated.
Why not just
library(xts)
x = xts(1:3650, Sys.Date()+1:3650)
x[.indexwday(x) == 3 &
.indexmday(x) >= 15 &
.indexmday(x) <= 21
]
If first Wednesday is on 1st then third is on 15th.
If first Wednesday is on 7th then third is on 21st.
So anywhere between 15th and 21st.
Take your wed object, split it by month, then select the 3rd row. Then use do.call and rbind to put it back together.
R> # 3rd or last available Wednesday
R> wedList <- split(wed, "months")
R> do.call(rbind, lapply(wedList, function(x) x[min(nrow(x),3),]))
# [,1]
# 2015-09-30 6
# 2015-10-21 27
# 2015-11-18 55
# 2015-12-16 83
R> # no observation if 3rd Wednesday isn't available
R> do.call(rbind, lapply(wedList, function(x) if(nrow(x) < 3) NULL else x[3,]))
# [,1]
# 2015-10-21 27
# 2015-11-18 55
# 2015-12-16 83