[R] matrix shifting

Berton Gunter gunter.berton at gene.com
Thu Mar 16 23:57:08 CET 2006


c[,2]<- c(NA,-as.numeric(x[-1,1]<x[-nrow(x),1])

You need to tack the NA on as there is no lagged value for the first row.

-- Bert Gunter
Genentech Non-Clinical Statistics
South San Francisco, CA
 
"The business of the statistician is to catalyze the scientific learning
process."  - George E. P. Box
 
 

> -----Original Message-----
> From: r-help-bounces at stat.math.ethz.ch 
> [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of 
> Gottfried Gruber
> Sent: Thursday, March 16, 2006 2:32 PM
> To: r-help
> Subject: [R] matrix shifting
> 
> hi,
> 
> somehow i can not figure it it, although it sounds simple. i 
> have a matrix and 
> i want one column to be laged (like the lag-operator).
> e.g.
> mymatrix[,2]=ifelse(mymatrix[,1]<lag(mymatrix[,1],-1),-1,0) 
> #i know this do 
> not work just for illustration
> 
> as a result i want to have in column 2 a -1 if the current 
> value (in the row) 
> is lower than the last value (from the row above) and otherwise zero.
> can that be done in one simple command, or do i have to use a loop?
> 
> thanks for any comments,
> tia gg
> -- 
> ---------------------------------------------------
> Gottfried Gruber
> mailto:gottfried.gruber at terminal.at
> www: http://gogo.sehrsupa.net
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! 
> http://www.R-project.org/posting-guide.html
>




More information about the R-help mailing list