[R] elements in each row of a matrix to the left.

Patrick Burns pburns at pburns.seanet.com
Mon Feb 27 20:55:00 CET 2006


John,

Does

t(apply(z, 1, sort, na.last=TRUE))

do what you want?


Patrick Burns
patrick at burns-stat.com
+44 (0)20 8525 0696
http://www.burns-stat.com
(home of S Poetry and "A Guide for the Unwilling S User")

john.gavin at ubs.com wrote:

>Hi,
>
>Given a matrix like
>
>(z <- matrix(c(
>1, 1, NA, NA, NA, NA,
>1,  NA, 1,  NA, 1, NA,
>NA, 1, 1,  1,  NA, NA), ncol = 3))
>
>     [,1] [,2] [,3]
>[1,]    1    1   NA
>[2,]    1   NA    1
>[3,]   NA    1    1
>[4,]   NA   NA    1
>[5,]   NA    1   NA
>[6,]   NA   NA   NA
>
>is there a vectorised way to produce the output like
>
>     [,1] [,2] [,3]
>[1,]    1    1   NA
>[2,]    1   NA    1
>[3,]    1    1   NA
>[4,]    1   NA   NA
>[5,]    1   NA   NA
>[6,]   NA   NA   NA
>
>That is, given an n by m matrix, and going row by row, 
>if the first non-NA element is in column k
>I want to move elements in columns from k to m
>to columns 1 to m-k+1 with NAs filling in from 
>m-k+2 to m.
>
>  
>
>>version
>>    
>>
>         _              
>platform i386-pc-mingw32
>arch     i386           
>os       mingw32        
>system   i386, mingw32  
>status                  
>major    2              
>minor    2.1            
>year     2005           
>month    12             
>day      20             
>svn rev  36812          
>language R        
>
>Regards,
>
>John.
>
>John Gavin <john.gavin at ubs.com>,
>Quantitative Risk Control,
>UBS Investment Bank, 6th floor, 
>100 Liverpool St., London EC2M 2RH, UK.
>Phone +44 (0) 207 567 4289
>Fax   +44 (0) 207 568 5352
>
>Visit our website at http://www.ubs.com
>
>This message contains confidential information and is intend...{{dropped}}
>
>______________________________________________
>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