[R] shifting data in matrix by n rows
jim holtman
jholtman at gmail.com
Thu Jul 10 03:58:00 CEST 2008
Is this what you want:
> x <- read.table(textConnection("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"), header=TRUE)
> closeAllConnections()
> x
Date Apples Oranges Pears
1 1/7 2 3 5
2 2/7 1 4 7
3 3/7 3 8 10
4 4/7 5 7 2
5 5/7 6 3 5
> # shift down one row
> x[, -1] <- rbind(NA, head(x[, -1], -1))
>
> x
Date Apples Oranges Pears
1 1/7 NA NA NA
2 2/7 2 3 5
3 3/7 1 4 7
4 4/7 3 8 10
5 5/7 5 7 2
>
On Wed, Jul 9, 2008 at 6:45 PM, rcoder <michael.larsson at gmail.com> wrote:
>
> Original Table:
> 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
>
> What I want after shift (data shifted, dates left unchanged)
> Date Apples Oranges Pears
> 1/7 na na na
> 2/7 2 3 5
> 3/7 1 4 7
> 4/7 3 8 10
> 5/7 5 7 2
>
>
>
> jholtman wrote:
>>
>> Can you provide commented, minimal, self-contained, reproducible code.
>> If you don't have code, at least provide a before/after version of
>> the matrix that you would like. It is easy to use indexing to move
>> stuff around, we just have to know what is it that you want to move.
>>
>> 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.
>>>
>>
>>
>>
>> --
>> Jim Holtman
>> Cincinnati, OH
>> +1 513 646 9390
>>
>> What is the problem you are trying to solve?
>>
>> ______________________________________________
>> 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-tp18368420p18372509.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.
>
--
Jim Holtman
Cincinnati, OH
+1 513 646 9390
What is the problem you are trying to solve?
More information about the R-help
mailing list