FAQ Database Discussion Community


data.table gives the error Error: k <= n is not TRUE

r,data.table,dplyr,zoo
I am trying to convert the following dplyr code to data.table equivalent. However, the data.table, but not the dplyr, gives me the error. Error: k <= n is not TRUE. #using dplyr library(dplyr) library(zoo) #rollmean function DF<-mtcars DF %>% filter(cyl==6)%>% group_by(am,vs) %>% mutate(cumsum_mpg=cumsum(mpg),cummin_disp=cummin(disp),rollmean_wt=rollmean(wt,k=2,fill=0,align="right")) #using data.table library(data.table) library(zoo) #rollmean function DT<-data.table(mtcars)...

How to apply regression to a zoo data frame in R?

r,curve-fitting,zoo
I have a zoo data frame, a piece of which looks like: TLT PCY SHY 2015-04-15 122.1545 28.16594 84.63730 2015-04-16 123.9836 28.11196 84.72825 2015-04-17 124.4341 28.07958 84.70804 2015-04-20 125.2258 28.15514 84.74845 2015-04-21 125.9629 28.18753 84.76866 2015-04-22 126.1267 28.28469 84.79897 2015-04-23 126.4923 28.30628 84.85056 2015-04-24 124.5478 28.34947 84.81010 2015-04-27 123.3290 28.36026...

Extracting event types from last 21 day window

r,dplyr,zoo
My dataframe looks like this. The two rightmost columns are my desired columns. **Name ActivityType ActivityDate Email(last 21 says) Webinar(last21)** John Email 1/1/2014 NA NA John Webinar 1/5/2014 NA NA John Sale 1/20/2014 Yes Yes John Webinar 3/25/2014 NA NA John Sale 4/1/2014 No Yes John Sale 7/1/2014 No No...

Rearranging the zoo::as.yearqtr() output

r,zoo
I know that I can call: library(zoo) Sys.Date() CQ <- as.yearqtr(Sys.Date()) CQ And get: "2015-03-05" "2015 Q1" But I need my output to be: "Q1-2015" How can I convert CQ to be QX-YYYY?...

Efficient way to perform running total in the last 365 day window

r,vectorization,dplyr,zoo,rollapply
This is what my data frame looks like: Name EventType EventDate SalesAmount RunningTotal Runningtotal(prior365Days) John Email 1/1/2014 0 0 0 John Sale 2/1/2014 10 10 10 John Sale 7/1/2014 20 30 30 John Sale 4/1/2015 30 60 50 John Webinar 5/1/2015 0 60 50 Tom Email 1/1/2014 0 0 0...

conditional cumulative sum using dplyr

r,dplyr,zoo
My dataframe looks like this and I want two separate cumulative columns, one for fund A and the other for fund B Name Event SalesAmount Fund Cum-A(desired) Cum-B(desired) John Webinar NA NA NA NA John Sale 1000 A 1000 NA John Sale 2000 B 1000 2000 John Sale 3000 A...

R: RunningTotal in the last 365 days window by Name

r,data.table,dplyr,zoo
This is what my data looks like. The rightmost column is my Desired Column. Name EventType EventDate SalesAmount RunningTotal Runningtotal(prior365Days) John Email 1/1/2014 0 0 0 John Sale 2/1/2014 10 10 10 John Sale 7/1/2014 20 30 30 John Sale 4/1/2015 30 60 50 John Webinar 5/1/2015 0 60 50...

Convert data frame with epoch timestamps to time-series with milliseconds in R

r,time-series,xts,zoo
I have the following data.frame: df <- data.frame(timestamp=c(1428319770511, 1428319797218, 1428319798182, 1428319803327, 1428319808478), session=c("A","A","B","A","A")) I'd like to convert this data frame to a time series and work on time windows shorter than one second. I already tried zoo and xts, but I found it difficult to represent the epoch times as...

Limiting interpolation function to NA values

r,interpolation,zoo,spline
I am trying to use na.spline, part of the zoo package, to replace NA values in some imported speed data with a cubic spline interpolated values. na.spline is modifying the NA values as it is supposed to; however, it is also modifying values that originally equaled 0. ex <- data.frame(speed)...

R: Setting limits to scale_x_yearqtr in ggplot for yearqtr (zoo)

r,ggplot2,time-series,zoo,as.date
I'm working with the data set resembling the extract below: head(nomis.lng.agg) quarter decile avg.val 1 2004 Q4 1 5.680000 2 2005 Q1 1 5.745763 3 2005 Q2 1 5.503341 4 2005 Q3 1 5.668224 5 2005 Q4 1 5.244604 6 2006 Q1 1 5.347222 The variable quarter is of class...

rollapply : Is it possible to add end date for each sliding window?

r,time-series,xts,zoo,rollapply
A dummy zoo object is created as z <- zoo(11:15, as.Date(31:45)) as.data.frame(z) z 1970-02-01 11 1970-02-02 12 1970-02-03 13 1970-02-04 14 1970-02-05 15 1970-02-06 11 1970-02-07 12 1970-02-08 13 1970-02-09 14 1970-02-10 15 1970-02-11 11 1970-02-12 12 1970-02-13 13 1970-02-14 14 1970-02-15 15 rollapply function can be used to calculate...

Replace -inf, NaN and NA values with zero in a dataset in R

r,time-series,nan,zoo
I am trying to run some trading strategies in R. I have downloaded some stock prices and calculated returns. The new return dataset has a number of -inf, NaN, and NA values. I am reproducing a row of the dataset (log_ret). Its a zoo dataset. library(zoo) log_ret <- structure( c(0.234,-0.012,-Inf,NaN,0.454,Inf),...

How to merge list of zoo objects

r,zoo
I have a list of zoo objects, and I'd like to merge them. I've already made sure that they're all the same length. It seems that in order to merge them, I need to do something like, merge(my_list[[1]],my_list[[2]]...) But this quickly becomes cumbersome as list length increases. Does someone have...

Moving window over zoo time series in R

r,zoo
I'm running into issues while applying a moving window function to a time series dataset. I've imported daily streamflow data (date and value) into a zoo object, as approximated by the following: library(zoo) df <- data.frame(sf = c("2001-04-01", "2001-04-02", "2001-04-03", "2001-04-04", "2001-04-05", "2001-04-06", "2001-04-07", "2001-06-01", "2001-06-02", "2001-06-03", "2001-06-04", "2001-06-05", "2001-06-06"),...

Finding index of a row in zoo object in R

indexing,zoo
I have a zoo object with index of POSIXct and it contains %Y-%m-%d %H:%M:%S My data is look like this: 2008-01-03 13:55:00 794.61 60.10000 2008-01-03 14:25:00 794.49 60.10000 2008-01-03 14:55:00 795.91 60.10000 2008-01-03 15:27:00 796.02 60.10000 2008-01-04 15:56:00 792.10 60.10000 2008-01-04 16:21:00 794.24 60.10000 2008-01-04 16:40:00 794.66 60.10000 2008-01-04 18:20:00...

R: rollapplyr and lm factor error: Does rollapplyr change variable class?

r,plyr,zoo,rollapply
This question builds upon a previous one which was nicely answered for me here. R: Grouped rolling window linear regression with rollapply and ddply Wouldn't you know that the code doesn't quite work when extended to the real data rather than the example data? I have a somewhat large dataset...

R - how to calculate “global” monthly means of a zoo object

r,time-series,mean,zoo
Let's say I have this zoo object: library(zoo) df <- structure(list(date = structure(c(0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365, 396, 424, 455, 485, 516, 546, 577, 608, 638, 669, 699, 730, 761, 790, 821, 851, 882, 912, 943, 974, 1004, 1035, 1065, 1096, 1127,...

dplyr mutate in zoo object

r,dplyr,zoo
I was trying to apply the dplyr mutate in zoo object. But, it generated an error: Error in UseMethod("mutate") : no applicable method for 'mutate' applied to an object of class "zoo". I googled and saw that it has not been yet resolved. A recent discussion on this is here....

Using dplyr summary function on yearmon from zoo

r,dplyr,zoo
I have a data frame with values associated to a year and month. I use yearmon class from zoo package to store the year-month info. My aim is to count the average of those values from the same year-month. However, using dplyr seems to give me an error. The variable...

Vectorize window.zoo over start= and end=

r,vectorization,zoo
I have input data that look like that (reduced to two time-series for the example). library(zoo) begin <- as.Date(c('2003-02-12', '2003-01-23')) end <- as.Date(c('2003-10-02', '2003-08-01')) x.Date <- as.Date("2003-01-01") + seq(1, 365, 8) - 1 data <- matrix(rnorm(length(x.Date)*2), ncol = 2, dimnames = list(r = NULL, col = c('a', 'b'))) I'm trying...

Filling higher resolution zoo obj with data from lower resolution zoo obj

r,xts,zoo
I have one zoo object with hourly observations, and one with daily observations. My goal is to merge the two series by the index into one object, where I match daily values with all hourly values of the same date. To be specific, the first object zX contains hourly observations...

R: Converting output from getSymbols() to data frame in one command without calling the object name explicitly

r,data.frame,xts,zoo,quantmod
I would like to to convert output from the getSymbols in quantmod package to a data frame. Presently I achieve that with the following code. Data <- new.env() getSymbols(Symbols = "EUR/USD", src = "oanda", from = "2005-01-01", to = "2006-01-01", env = Data) test <- as.data.frame(Data$EURUSD) head(test) Ideally, I would...

Create a new zoo object based on a vector of variable names

r,get,lag,zoo
Set seed and variables. Please assume all everything is this section is given and unalterable. library(zoo) set.seed(123) a <- zoo(rnorm(10), order.by = as.Date(50:60)) b <- zoo(rnorm(10), order.by = as.Date(50:60)) c <- zoo(rnorm(10), order.by = as.Date(50:60)) lags <- c(1,3,1) variables <- c("a","c","b") I want to create an adequate zoo object that...

How to rollapply array along 3rd dimension?

r,zoo
Say that I have the array: > arr <- array(1, dim=c(3, 3, 3)) , , 1 [,1] [,2] [,3] [1,] 1 1 1 [2,] 1 1 1 [3,] 1 1 1 , , 2 [,1] [,2] [,3] [1,] 1 1 1 [2,] 1 1 1 [3,] 1 1 1 ,...

R - remove “one after another” duplicates

r,duplicates,zoo
I try to find a way to remove succesive duplicates in R language. I have a zoo object like: 2015-01-01 12:00:00 1 2015-01-01 13:00:00 1 2015-01-01 14:00:00 1 2015-01-01 15:30:00 4 2015-01-01 16:00:00 1 2015-01-01 17:00:00 6 and my expected result is: 2015-01-01 12:00:00 1 2015-01-01 15:30:00 4 2015-01-01 16:00:00...

Why is zoo::rollmean slow compared to a simple Rcpp implementation?

r,zoo,moving-average
zoo::rollmean is a helpful function that returns the rolling mean of a time series; for vector x of length n and window size k it returns the vector c(mean(x[1:k]), mean(x[2:(k+1)]), ..., mean(x[(n-k+1):n])). I noticed that it seemed to be running slowly for some code I was developing, so I wrote...