[R] how to track a number in a row

David Winsemius dwinsemius at comcast.net
Tue Jan 12 04:16:31 CET 2010


On Jan 11, 2010, at 9:38 PM, Márcio Resende wrote:

>
> Hi,
> I have a 100x15 matrix and in each row a set of 15 random numbers  
> out of 25.
> for example:
>
> b <- c(1:25)
> a <-matrix(0,10,15)
> for (i in 1:10){
> a[i,] <- sample(b,15,replace = FALSE)
> }
>
> I would like to create another matrix (25x100), for example "d" with  
> the
> probability of each number from the first matrix
> Therefore I need to track, for example, if number 1 is present in  
> the first
> row (d[1,1]) (which would give me an probability of 1 out of 1).
> Then, track again if number 1 is present on the second row (d[2,1])  
> (And if
> not, the probability would be 1 out of 2 = 0.5)...and so on for all  
> the 25
> collumns (25 numbers) and all the 100 rows.
>
> Could anybody help how to do it??

Maybe:

 > apply(a, 2, table)[[1]]

  1  5  6  7 10 16 21 22 25
  1  2  1  1  1  1  1  1  1
 > apply(a, 2, table)[[1]]/10

   1   5   6   7  10  16  21  22  25
0.1 0.2 0.1 0.1 0.1 0.1 0.1 0.1 0.1
 > lapply(apply(a, 2, table), "/", 10)
[[1]]

   1   5   6   7  10  16  21  22  25
0.1 0.2 0.1 0.1 0.1 0.1 0.1 0.1 0.1

[[2]]

   1   2   7   9  12  15  16  17  25
0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.2


snipped remaining 13 vectors in the 15 element list

 > str(lapply(apply(a, 2, table), "/", 10)[[1]])
  table [1:9(1d)] 0.1 0.2 0.1 0.1 0.1 0.1 0.1 0.1 0.1
  - attr(*, "dimnames")=List of 1
   ..$ : chr [1:9] "1" "5" "6" "7" ...
 > as.numeric(names(lapply(apply(a, 2, table), "/", 10)[[1]]))
[1]  1  5  6  7 10 16 21 22 25


 > d <- matrix(0, nrow=25, ncol=15)
 > d[as.numeric(names(lapply(apply(a, 2, table), "/", 10)[[1]])), 1]  
<- lapply(apply(a, 2, table), "/", 10)[[1]]
 > d
       [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12]  
[,13] [,14] [,15]
  [1,]  0.1    0    0    0    0    0    0    0    0     0     0      
0     0     0     0
  [2,]  0.0    0    0    0    0    0    0    0    0     0     0      
0     0     0     0
  [3,]  0.0    0    0    0    0    0    0    0    0     0     0      
0     0     0     0
  [4,]  0.0    0    0    0    0    0    0    0    0     0     0      
0     0     0     0
  [5,]  0.2    0    0    0    0    0    0    0    0     0     0      
0     0     0     0
  [6,]  0.1    0    0    0    0    0    0    0    0     0     0      
0     0     0     0
  [7,]  0.1    0    0    0    0    0    0    0    0     0     0      
0     0     0     0
  [8,]  0.0    0    0    0    0    0    0    0    0     0     0      
0     0     0     0
  [9,]  0.0    0    0    0    0    0    0    0    0     0     0      
0     0     0     0
[10,]  0.1    0    0    0    0    0    0    0    0     0     0      
0     0     0     0
[11,]  0.0    0    0    0    0    0    0    0    0     0     0      
0     0     0     0
[12,]  0.0    0    0    0    0    0    0    0    0     0     0      
0     0     0     0
[13,]  0.0    0    0    0    0    0    0    0    0     0     0      
0     0     0     0
[14,]  0.0    0    0    0    0    0    0    0    0     0     0      
0     0     0     0
[15,]  0.0    0    0    0    0    0    0    0    0     0     0      
0     0     0     0
[16,]  0.1    0    0    0    0    0    0    0    0     0     0      
0     0     0     0
[17,]  0.0    0    0    0    0    0    0    0    0     0     0      
0     0     0     0
[18,]  0.0    0    0    0    0    0    0    0    0     0     0      
0     0     0     0
[19,]  0.0    0    0    0    0    0    0    0    0     0     0      
0     0     0     0
[20,]  0.0    0    0    0    0    0    0    0    0     0     0      
0     0     0     0
[21,]  0.1    0    0    0    0    0    0    0    0     0     0      
0     0     0     0
[22,]  0.1    0    0    0    0    0    0    0    0     0     0      
0     0     0     0
[23,]  0.0    0    0    0    0    0    0    0    0     0     0      
0     0     0     0
[24,]  0.0    0    0    0    0    0    0    0    0     0     0      
0     0     0     0
[25,]  0.1    0    0    0    0    0    0    0    0     0     0      
0     0     0     0

So that "worked for one column. Too bad the R syntax does not allow  
slightly more flexible handling of indices or this might have worked:
 > d[as.numeric(names(lapply(apply(a, 2, table), "/", 10)[[1:15]])),  
1] <- lapply(apply(a, 2, table), "/", 10)[[1:15]]
Error in lapply(apply(a, 2, table), "/", 10)[[1:15]] :
   recursive indexing failed at level 2

As it is a loop does the job:



 > for(i in 1:15) {d[as.numeric(names(lapply(apply(a, 2, table), "/",  
10)[[i]])), i] <- lapply(apply(a, 2, table), "/", 10)[[i]]}
 > d
       [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12]  
[,13] [,14] [,15]
  [1,]  0.1  0.1  0.1  0.1  0.0  0.0  0.0  0.1  0.0   0.0   0.1    
0.1   0.1   0.0   0.0
  [2,]  0.1  0.1  0.1  0.0  0.1  0.0  0.0  0.0  0.0   0.0   0.1    
0.0   0.0   0.0   0.0
  [3,]  0.1  0.0  0.0  0.0  0.1  0.0  0.0  0.0  0.1   0.2   0.0    
0.1   0.0   0.0   0.0
  [4,]  0.1  0.0  0.0  0.2  0.0  0.0  0.2  0.0  0.0   0.0   0.0    
0.0   0.0   0.0   0.1
  [5,]  0.2  0.0  0.1  0.0  0.1  0.0  0.0  0.0  0.0   0.0   0.0    
0.0   0.0   0.0   0.3
  [6,]  0.1  0.0  0.0  0.0  0.0  0.0  0.1  0.1  0.0   0.0   0.0    
0.0   0.2   0.1   0.0
  [7,]  0.1  0.1  0.0  0.1  0.0  0.0  0.0  0.1  0.1   0.0   0.2    
0.0   0.0   0.0   0.0
  [8,]  0.1  0.0  0.0  0.1  0.0  0.1  0.0  0.0  0.1   0.1   0.0    
0.1   0.0   0.1   0.0
  [9,]  0.1  0.1  0.0  0.0  0.0  0.0  0.0  0.0  0.2   0.0   0.0    
0.0   0.0   0.1   0.0
[10,]  0.1  0.0  0.0  0.1  0.1  0.0  0.0  0.0  0.0   0.1   0.1   0.0    
0.0   0.2   0.0
[11,]  0.1  0.0  0.0  0.1  0.1  0.0  0.0  0.1  0.0   0.0   0.2   0.0    
0.1   0.0   0.0
[12,]  0.2  0.1  0.1  0.0  0.1  0.0  0.0  0.1  0.0   0.2   0.0   0.0    
0.0   0.0   0.0
[13,]  0.1  0.0  0.1  0.0  0.0  0.1  0.0  0.1  0.0   0.0   0.1   0.1    
0.0   0.1   0.0
[14,]  0.1  0.0  0.1  0.0  0.1  0.0  0.1  0.0  0.1   0.0   0.0   0.1    
0.0   0.1   0.1
[15,]  0.1  0.1  0.0  0.1  0.0  0.0  0.0  0.1  0.0   0.1   0.0   0.0    
0.0   0.0   0.0
[16,]  0.1  0.1  0.0  0.1  0.0  0.2  0.0  0.0  0.0   0.0   0.0   0.0    
0.0   0.0   0.0
[17,]  0.2  0.1  0.0  0.0  0.0  0.1  0.0  0.0  0.1   0.0   0.0   0.1    
0.2   0.0   0.0
[18,]  0.1  0.0  0.0  0.0  0.0  0.1  0.2  0.0  0.0   0.1   0.0   0.2    
0.1   0.1   0.0
[19,]  0.1  0.0  0.1  0.0  0.1  0.1  0.2  0.1  0.1   0.0   0.1   0.0    
0.0   0.0   0.0
[20,]  0.1  0.0  0.1  0.0  0.0  0.0  0.0  0.0  0.0   0.1   0.0   0.1    
0.0   0.1   0.1
[21,]  0.1  0.0  0.0  0.0  0.2  0.1  0.0  0.0  0.0   0.0   0.1   0.0    
0.0   0.0   0.1
[22,]  0.1  0.0  0.1  0.0  0.0  0.0  0.0  0.0  0.0   0.0   0.0   0.1    
0.2   0.0   0.0
[23,]  0.1  0.0  0.0  0.0  0.0  0.0  0.1  0.1  0.2   0.1   0.0   0.0    
0.1   0.0   0.0
[24,]  0.2  0.0  0.0  0.1  0.0  0.1  0.1  0.0  0.0   0.0   0.0   0.0    
0.0   0.1   0.2
[25,]  0.1  0.2  0.1  0.0  0.0  0.1  0.0  0.1  0.0   0.0   0.0   0.0    
0.0   0.0   0.1
 >
> Thanks in advance
> Marcio
>
>
> -- 


David Winsemius, MD
Heritage Laboratories
West Hartford, CT



More information about the R-help mailing list