[R] shifting data in matrix by n rows

Gabor Grothendieck ggrothendieck at gmail.com
Fri Jul 11 12:52:24 CEST 2008


Just one additional item.  Since I posted, an optional
na.pad= argument has been added to lag.zooreg in the
devel version of zoo with which the code reduces to:

# next line grabs lag.zooreg from devel version of zoo
source("http://r-forge.r-project.org/plugins/scmsvn/viewcvs.php/*checkout*/pkg/R/window.zoo.R?rev=490&root=zoo")

zr <- as.zooreg(z)
lag(zr, -1, na.pad = TRUE)

On Thu, Jul 10, 2008 at 6:03 PM, Gabor Grothendieck
<ggrothendieck at gmail.com> wrote:
> Actually my last reply will drop one row since its pushed off
> to beyond the data range.  You can avoid that with zooreg:
>
> # from before for comparison
> lag(z, -1, na.pad = TRUE)
>
> # pure shift - note use of zooreg here
> # Unlike lag.zoo, lag.zooreg can shift beyond data range
> zr <- as.zooreg(z)
> lag(zr, -1)
>
> # lag.zooreg does not have an na.pad= arg but we
> # can merge it with original times to get NA row.
> merge(lag(zr, -1), zoo(, time(zr)))
>
> On Thu, Jul 10, 2008 at 5:52 PM, Gabor Grothendieck
> <ggrothendieck at gmail.com> wrote:
>> See ?lag and in zoo ?lag.zoo.  Both pages have
>> examples. Using lag.zoo here it is with your data:
>>
>> Lines <- "Date   Apples   Oranges   Pears
>> 1/7     2          3            5
>> 2/7     1          4            7
>> 3/7     3          8            10
>> 4/7     5          7            2
>> 5/7     6          3            5"
>>
>> library(zoo)
>>
>> # z <- read.zoo("myfile.dat", header = TRUE, format = "%d/%m")
>> z <- read.zoo(textConnection(Lines), header = TRUE, format = "%d/%m")
>>
>> lag(z, -1, na.pad = TRUE)
>>
>> On Thu, Jul 10, 2008 at 3:12 PM, rcoder <michael.larsson at gmail.com> wrote:
>>>
>>> Hi everyone,
>>>
>>> Thanks very much for all your replies.
>>>
>>> I'm interested in hearing more about the lag function. I remember coming
>>> across this in the R intro manual, but I couldn't figure out how to apply it
>>> to my case. Does anyone know how it is applied, assuming a time series data
>>> frame?
>>>
>>> Thanks,
>>>
>>> rcoder
>>>
>>>
>>>
>>> Gabor Grothendieck wrote:
>>>>
>>>> If its a zoo or ts time series you can use the lag function.
>>>>
>>>> On Wed, Jul 9, 2008 at 2:57 PM, rcoder <michael.larsson at gmail.com> wrote:
>>>>>
>>>>> Hi everyone,
>>>>>
>>>>> I have some data in a matrix, and I want to shift it down by one row. The
>>>>> matrix in question has a date column. Does anyone know of a way to shift
>>>>> the
>>>>> data by one row, whilst preserving the date column in the matrix - i.e.
>>>>> shift the data and leave the date column in the current location?
>>>>>
>>>>> Thanks,
>>>>>
>>>>> rcoder
>>>>> --
>>>>> View this message in context:
>>>>> http://www.nabble.com/shifting-data-in-matrix-by-n-rows-tp18368420p18368420.html
>>>>> Sent from the R help mailing list archive at Nabble.com.
>>>>>
>>>>> ______________________________________________
>>>>> R-help at 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 at 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/shifting-data-in-matrix-by-n-rows-tp18368420p18389841.html
>>> Sent from the R help mailing list archive at Nabble.com.
>>>
>>> ______________________________________________
>>> R-help at 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.
>>>
>>
>



More information about the R-help mailing list