[R] Finding all rows of a matrix equal to vector

Berend Hasselman bhh at xs4all.nl
Sat Jul 16 14:14:56 CEST 2011


djmuseR wrote:
> 
> Hi:
> 
> To take this idea a step further,
> 
> colSums(apply(unique(A), 1, function(x) apply(A, 1, function(y)
> identical(x, y))))
> [1] 10  5  5
> 
> However, Denis' solution is a bit faster because fewer evaluations are
> required:
> 
>> system.time(replicate(1000,
> + colSums(apply(unique(A), 1, function(x) apply(A, 1, function(y)
> identical(x, y))))
> +   ))
>    user  system elapsed
>    1.42    0.00    1.42
>> system.time(replicate(1000, {B <- apply(A,1, FUN = function(x)paste(x,
>> collapse = ''))
> + table(B) }))
>    user  system elapsed
>    0.81    0.00    0.81
> 


> system.time(replicate(1000,{sum(apply(A,1, FUN =
> function(x)identical(x,x1)))}))
   user  system elapsed 
  0.301   0.001   0.305 
> 
> system.time(replicate(1000,{sum(apply(A,1, FUN =
> function(x)all(zapsmall(x-x1)==0)))}))
   user  system elapsed 
  0.672   0.002   0.675 
> 
> system.time(replicate(1000, {B <- apply(A,1, FUN = function(x)paste(x,
> collapse = '')) ; table(B) })) 
   user  system elapsed 
  0.835   0.002   0.837 

Berend


--
View this message in context: http://r.789695.n4.nabble.com/Finding-all-rows-of-a-matrix-equal-to-vector-tp3671606p3671749.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list