Thanks for advice, sorted now!


________________________________
From: Owe Jessen-2 [via R] <ml-node+1819075-790408820-225...@n4.nabble.com>

Sent: Fri, 9 April, 2010 18:39:53
Subject: Re: How to replace all non-maximum values in a row with 0

Am 09.04.2010 10:04, schrieb burgundy: 

> Hi, 
> 
> I would like to replace all the max values per row with "1" and all other 
> values with "0". If there are two max values, then "0" for both. Example: 
> 
> from: 
> 2  3  0  0  200 
> 30 0  0  2  50 
> 0  0  3  0  0 
> 0  0  8  8  0 
> 
> to: 
> 0  0  0  0  1 
> 0  0  0  0  1 
> 0  0  1  0  0 
> 0  0  0  0  0 
> 
> Thanks! 
>     Nice little homework to get the day started. :-) 

This worked for me, but is probably not the shortest possible answer 

A <- matrix (c(2,  3,  0,  0,  200, 30, 0,  0,  2,  50, 0,  0,  3,  0,   
0, 0,  0,  8,  8,  0), nrow = 4, byrow=T) 
nr <- nrow(A) 
nc <- ncol(A) 
B <- matrix(0,nrow=nr, ncol=nc) 
for(i in 1:nr){ 
x <- which(A[i,]==max(A[i,])) 
B[i,x] <- 1 
if(sum(B[i,])>1) B[i,] <- as.vector(rep(0,nc)) 
} 

-- 
Owe Jessen 
Nettelbeckstr. 5 
24105 Kiel 
[hidden email] 
http://privat.owejessen.de

______________________________________________ 
[hidden email] 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 message @ 
http://n4.nabble.com/How-to-replace-all-non-maximum-values-in-a-row-with-0-tp1819018p1819075.html
 
To unsubscribe from How to replace all non-maximum values in a row with 0, 
click here. 




-- 
View this message in context: 
http://r.789695.n4.nabble.com/How-to-replace-all-non-maximum-values-in-a-row-with-0-tp1819018p2076546.html
Sent from the R help mailing list archive at Nabble.com.

        [[alternative HTML version deleted]]

______________________________________________
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.

Reply via email to