[R] Vector comparison to matrix

From: Sigurdur Jonsson <sigurdur_at_hafro.is>
Date: Tue 16 Aug 2005 - 20:47:06 EST


Hi Todd and list,

I see you have received a few suggestions, here's another:

# set up data: your vector and an a 3x300000 matrix with a few
# matching lines:

target<-c(1,2,3)
A<-matrix(sample(1:3,300000,replace=TRUE),ncol=3)

# count matches:

nMatches<-sum(apply(A,1,function(x,target)   all.equal(x,target),target)=="TRUE")

# by applying a simple function, which takes 'target' as an 'extra'
# argument, to the rows of A. The function returns a vector of
# differences and 'TRUE'-s, the latter of which can be counted.

This took 1-2 minutes on my >3 year old laptop.

Siggi

> version

         _
platform i686-redhat-linux-gnu

arch     i686
os       linux-gnu

system i686, linux-gnu
status
major 2
minor 0.0
year 2004
month 10
day 04
language R

Yeah, I know, an update is (over)due.

-- 
-----------------------------------------------------------------------------
Sigurđur Ţór Jónsson	/	Sigurdur Tor Jonsson
E-mail:				<sigurdur@hafro.is>			
Snail-mail:			Marine Research Institute,
				P.O. Box 1390,
				121 Reykjavik,Iceland
Telephone (direct line):	+354 5752093
Telephone (switchboard):	+354 5752000 
Fax:				+354 5752001

______________________________________________
R-help@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
Received on Tue Aug 16 20:56:12 2005

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:39:49 EST