[R] How to know the row number of raw matrix after r

Petr Pikal petr.pikal at precheza.cz
Wed May 12 17:08:50 CEST 2004


Sorry, last attempt, before contacting our computer people.

Well, I am not sure what was wrong but my reply to Taemyong Choi's question  
was somehow mismatched so I try again. 

Hi  

On 12 May 2004 at 22:35, Taemyong Choi wrote:  

> > DATA  
>   
>             [,1]  
>   
>  [1,] -2.0314185  
>   
>  [2,] -0.3594015  
>   
>  [3,] -1.3225832  
>   
>  [4,]  1.5050892  
>   
>  [5,] -0.4535558  
>   
>  [6,]  3.6335036  
>   
>  [7,]  5.1965750  
>   
>  [8,]  4.2923129  
>   
>  [9,]  6.0913187  
>   
> [10,]  4.9131070  
>   
> > RESA  
>   
>            [,1]       [,2]       [,3]       [,4]       [,5]  
>   
> [1,] -2.0314185  1.5050892 -1.3225832 -1.3225832 -2.0314185  
>   
> [2,] -0.3594015 -0.3594015  4.2923129 -0.3594015 -0.4535558  
>   
> [3,]  4.9131070  4.9131070 -2.0314185  4.9131070  3.6335036  
>   
> [4,]  3.6335036 -0.4535558  3.6335036  1.5050892 -0.3594015  
>   
> [5,]  1.5050892  6.0913187 -0.3594015  4.2923129  6.0913187  
>   
> [6,]  6.0913187  3.6335036  5.1965750 -2.0314185  5.1965750  
>   
> [7,] -0.4535558  5.1965750  6.0913187  3.6335036  4.9131070  
>   
> First,  
>   
> RESA matrix consists of resampling DATA matrix. I would like to know  
> the row number of RESA matrix's value in DATA matrix.  
>   
> For example, RESA[1,1] = -2.0314185 and its row number in DATA matrix  
> is 1.  
>   
>                   RESA[1,3]=4.9131070 and its row number in DATA  
>                   matrix is  
> 10.  

set.seed(1)  
x<-runif(10)  
y<-rep(sample(x,7),5)  
y<-matrix(y,7,5,byrow=T)  
match(y,x)  
[1] 10  8  2  5  4  9  1  5  4  9  1 10  8  2  1 10  8  2  5  4  9  2  5  4  9  1 10  8  9    
1 10  8  2  5  4  


>   
>   
>   
> Is there a simple way to know the row number?  
>   
> I  
>   
> > clus  
>   
>      col1 col2 col3 col4 col5  
>   
> [1,]    1    1    1    1    1  
>   
> [2,]    2    2    0    2    1  
>   
> [3,]    2    1    1    2    2  
>   
> [4,]    1    2    2    1    2  
>   
> [5,]    3    1    2    2    3  
>   
> [6,]    3    3    0    3    2  
>   
> [7,]    1    3    2    0    0  

>   
> Second, I want to count columns of each row without 0  
>   
> i.e. The number of clus[2,]'s columns is 4.   
>   

2.  
     [,1] [,2] [,3] [,4] [,5]  
[1,]    1    3    1    5    0  
[2,]    0    1    2    0    1  
[3,]    4    3    4    5    4  
[4,]    2    1    1    3    0  
[5,]    5    4    1    2    5  
> clus<-matrix(clus,5,5,byrow=T)  
> clus>0  
      [,1] [,2] [,3]  [,4]  [,5]  
[1,]  TRUE TRUE TRUE  TRUE FALSE  
[2,] FALSE TRUE TRUE FALSE  TRUE  
[3,]  TRUE TRUE TRUE  TRUE  TRUE  
[4,]  TRUE TRUE TRUE  TRUE FALSE  
[5,]  TRUE TRUE TRUE  TRUE  TRUE  
> rowSums(clus>0)  
[1] 4 3 5 4 5  

>   
>   
> I solve this with next code.  
>   
> number <- c(rep(1,5))  # 5 is total column number.  
>   
> sum(number[clus[2,]!=0])   
>   
> Is there more simple method?  
>   
>   
>   
> Finally, clus[2,]'s mode is 2. I try to find function to know mode,  
> but I have failed.  

Maybe names(which.max(table(clus))) is what you want?  

> table(clus)  
clus  
0 1 2 3 4 5   
4 7 3 3 4 4   

Cheers  
Petr  


>   
> What's the function to know mode?  
>   
Petr Pikal
petr.pikal at precheza.cz




More information about the R-help mailing list