Re: [R] lag function row name with
Hi John, dplyr::lag expects a vector. The following should work dplyr::lag(temp[,1],2) HTH, Eric On Mon, Mar 25, 2019 at 9:45 AM John wrote: > Hi, > >I have a dataset whose row names corresponds to months. When I apply lag > function (dplyr package) on this dataset, I get NAs with warning messages. > Is there any lag function that carries out the lag but keep the row names? >I will have two datasets. The dates of the datasets are not exactly the > same, and I want to find out the correlation for the overlapping period. > >Thanks, > > > > temp > oil95 > 1981M01 103.27 > 1981M02 107.92 > 1981M03 110.26 > 1981M04 110.26 > 1981M05 110.11 > 1981M06 109.93 > 1981M07 109.93 > 1981M08 109.93 > 1981M09 109.93 > 1981M10 109.93 > > > dplyr::lag(temp, 2) > oil95 > 1981M01NA > 1981M02 > 1981M03 > 1981M04 > 1981M05 > 1981M06 > 1981M07 > 1981M08 > 1981M09 > 1981M10 > Warning message: > In format.data.frame(x, digits = digits, na.encode = FALSE) : > corrupt data frame: columns will be truncated or padded with NAs > > > > [[alternative HTML version deleted]] > > __ > R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. > [[alternative HTML version deleted]] __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] lag function row name with
Hi, I have a dataset whose row names corresponds to months. When I apply lag function (dplyr package) on this dataset, I get NAs with warning messages. Is there any lag function that carries out the lag but keep the row names? I will have two datasets. The dates of the datasets are not exactly the same, and I want to find out the correlation for the overlapping period. Thanks, > temp oil95 1981M01 103.27 1981M02 107.92 1981M03 110.26 1981M04 110.26 1981M05 110.11 1981M06 109.93 1981M07 109.93 1981M08 109.93 1981M09 109.93 1981M10 109.93 > dplyr::lag(temp, 2) oil95 1981M01NA 1981M02 1981M03 1981M04 1981M05 1981M06 1981M07 1981M08 1981M09 1981M10 Warning message: In format.data.frame(x, digits = digits, na.encode = FALSE) : corrupt data frame: columns will be truncated or padded with NAs > [[alternative HTML version deleted]] __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Lag function
Well, maybe. Whether it's +24 or -24 depends on what you mean by "one day lag." I suspect you mean -24, but perhaps this will help you decide: test <- ts(1:72, frequency = 24) plot(lag(test,24)) plot(lag(test,-24)) Note that the +24 moves the time base back 24 observable units (= hours) and -24 moves it forward 24 hours. This means that the day 2 observations are those from day 1, etc., which is usually what is wanted for lag. But you decide. Cheers, Bert Bert Gunter "The trouble with having an open mind is that people keep coming along and sticking things into it." -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip ) On Tue, Aug 7, 2018 at 7:47 AM, Edoardo Silvestri < silvestri.cas...@gmail.com> wrote: > I have an hourly database and I defined a variable as follows: > time<-ts(data$variable, frequency=24) > > If i need to create the variables with one day lag, the corresponding > command is lag(time,24)? > > Thank you > > [[alternative HTML version deleted]] > > __ > R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/ > posting-guide.html > and provide commented, minimal, self-contained, reproducible code. > [[alternative HTML version deleted]] __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] Lag function
I have an hourly database and I defined a variable as follows: time<-ts(data$variable, frequency=24) If i need to create the variables with one day lag, the corresponding command is lag(time,24)? Thank you [[alternative HTML version deleted]] __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] Lag Function
Can anyone tell me what's going on here? x - matrix(data=c(1,2,3,4,5),ncol=1) x1 - lag(x,k=1) x x1 x - x1 That's with x specified as a column vector, but the same thing happens when it's a row vector. x - c(1,2,3,4,5) x1 - lag(x,k=1) x x1 x - x1 When the documentation says Vector or matrix arguments x are coerced to time series. What does that mean? Thank you, Mitch __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Lag Function
Hello, Downey, Patrick wrote: Can anyone tell me what's going on here? x - matrix(data=c(1,2,3,4,5),ncol=1) x1 - lag(x,k=1) x x1 x - x1 That's with x specified as a column vector, but the same thing happens when it's a row vector. x - c(1,2,3,4,5) x1 - lag(x,k=1) x x1 x - x1 I'm not sure what you're expecting to happen. Can you clarify what needs explaining? My guess is that the 'lag' function is not doing what you expect, but you don't say what you expect. When the documentation says Vector or matrix arguments x are coerced to time series. What does that mean? Time series are a class of objects in R, it means if you don't pass the lag function a time series object, it's going to try to turn it into one. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Lag Function
It seems to mean that it adds a Tsp attribute but it does not change the class to ts: dput(lag(1:3)) structure(1:3, .Tsp = c(0, 2, 1)) Try this: ts(1:3) - structure(lag(1:3), class = ts) Time Series: Start = 1 End = 2 Frequency = 1 [1] -1 -1 or ts(1:3) - lag(ts(1:3)) Time Series: Start = 1 End = 2 Frequency = 1 [1] -1 -1 On Mon, Mar 22, 2010 at 12:15 PM, Downey, Patrick pdow...@urban.org wrote: Can anyone tell me what's going on here? x - matrix(data=c(1,2,3,4,5),ncol=1) x1 - lag(x,k=1) x x1 x - x1 That's with x specified as a column vector, but the same thing happens when it's a row vector. x - c(1,2,3,4,5) x1 - lag(x,k=1) x x1 x - x1 When the documentation says Vector or matrix arguments x are coerced to time series. What does that mean? Thank you, Mitch __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] lag function doesn't work - what am i doing wrong?
I am trying to lag a time series. My data is in a matrix, but I coerce it into a ts object. But when I lag it and then look at the result, nothing has changed. What am I doing wrong? residsq-resid^2 residsq-as.ts(residsq) lag1residsq-lag(residsq,-1) residsq[1:5] 1 2 3 4 5 87.759 329882.188 5325849.333 31512.334 70865.228 lag1residsq[1:5] 1 2 3 4 5 87.759 329882.188 5325849.333 31512.334 70865.228 PS: I tried to lag the series manually using rbind(0, residsq), but the result ended up returning me [ 0, residsq(1), 0, residsq(2), 0, ] WTF how do you stack 1 element onto another in R? -- View this message in context: http://www.nabble.com/lag-function-doesn%27t-work---what-am-i-doing-wrong--tp19922651p19922651.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] lag function doesn't work - what am i doing wrong?
lag1resid is a time series, try str(residsq) str(lag1residsq) The problem is that when you subscript lag1resid you don't get a time series out from that. See ?window.ts or try the zoo or xts packages where subscripting of time series works. On Fri, Oct 10, 2008 at 1:27 PM, Scotty Nelson [EMAIL PROTECTED] wrote: I am trying to lag a time series. My data is in a matrix, but I coerce it into a ts object. But when I lag it and then look at the result, nothing has changed. What am I doing wrong? residsq-resid^2 residsq-as.ts(residsq) lag1residsq-lag(residsq,-1) residsq[1:5] 1 2 3 4 5 87.759 329882.188 5325849.333 31512.334 70865.228 lag1residsq[1:5] 1 2 3 4 5 87.759 329882.188 5325849.333 31512.334 70865.228 PS: I tried to lag the series manually using rbind(0, residsq), but the result ended up returning me [ 0, residsq(1), 0, residsq(2), 0, ] WTF how do you stack 1 element onto another in R? -- View this message in context: http://www.nabble.com/lag-function-doesn%27t-work---what-am-i-doing-wrong--tp19922651p19922651.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] lag function doesn't work - what am i doing wrong?
thanks. i'll try zoo or xts. ts is a p.i.t.a. Gabor Grothendieck wrote: lag1resid is a time series, try str(residsq) str(lag1residsq) The problem is that when you subscript lag1resid you don't get a time series out from that. See ?window.ts or try the zoo or xts packages where subscripting of time series works. On Fri, Oct 10, 2008 at 1:27 PM, Scotty Nelson [EMAIL PROTECTED] wrote: I am trying to lag a time series. My data is in a matrix, but I coerce it into a ts object. But when I lag it and then look at the result, nothing has changed. What am I doing wrong? residsq-resid^2 residsq-as.ts(residsq) lag1residsq-lag(residsq,-1) residsq[1:5] 1 2 3 4 5 87.759 329882.188 5325849.333 31512.334 70865.228 lag1residsq[1:5] 1 2 3 4 5 87.759 329882.188 5325849.333 31512.334 70865.228 PS: I tried to lag the series manually using rbind(0, residsq), but the result ended up returning me [ 0, residsq(1), 0, residsq(2), 0, ] WTF how do you stack 1 element onto another in R? -- View this message in context: http://www.nabble.com/lag-function-doesn%27t-work---what-am-i-doing-wrong--tp19922651p19922651.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- View this message in context: http://www.nabble.com/lag-function-doesn%27t-work---what-am-i-doing-wrong--tp19922651p19923295.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] lag function doesn't work - what am i doing wrong?
Note that there is actually a good reason why this is so. ts can only represent regularly spaced series but the result of a subscripting operation might not be a regularly spaced series so you would not be able to guarantee that it could be a ts series as well. That's the reason for window.ts too since window.ts is constructed in such a way that the result is always a regular series so it can be presented as a ts object. The reason that zoo and xts can handle this is that they work with irregularly spaced series and subscripting an irregularly spaced series always creates another. For example: library(zoo) z - zoo(11:15) z[c(1, 3, 4)] The result of the last line is another zoo object but it would be impossible to do that with ts since the result in this case the result is irregularly spaced. On Fri, Oct 10, 2008 at 2:11 PM, Scotty Nelson [EMAIL PROTECTED] wrote: thanks. i'll try zoo or xts. ts is a p.i.t.a. Gabor Grothendieck wrote: lag1resid is a time series, try str(residsq) str(lag1residsq) The problem is that when you subscript lag1resid you don't get a time series out from that. See ?window.ts or try the zoo or xts packages where subscripting of time series works. On Fri, Oct 10, 2008 at 1:27 PM, Scotty Nelson [EMAIL PROTECTED] wrote: I am trying to lag a time series. My data is in a matrix, but I coerce it into a ts object. But when I lag it and then look at the result, nothing has changed. What am I doing wrong? residsq-resid^2 residsq-as.ts(residsq) lag1residsq-lag(residsq,-1) residsq[1:5] 1 2 3 4 5 87.759 329882.188 5325849.333 31512.334 70865.228 lag1residsq[1:5] 1 2 3 4 5 87.759 329882.188 5325849.333 31512.334 70865.228 PS: I tried to lag the series manually using rbind(0, residsq), but the result ended up returning me [ 0, residsq(1), 0, residsq(2), 0, ] WTF how do you stack 1 element onto another in R? -- View this message in context: http://www.nabble.com/lag-function-doesn%27t-work---what-am-i-doing-wrong--tp19922651p19922651.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- View this message in context: http://www.nabble.com/lag-function-doesn%27t-work---what-am-i-doing-wrong--tp19922651p19923295.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.