[R] counting matched elements in two vectors

2014-01-23 Thread M.Bezabih
   Hi all,
I have the following reproducible example

z-c(-5:40)
w-c(11, 11, 12, 14, 14, 14, 15, 16, 18, 25, 26, 26, 26, 27, 27, 30)
 r-z %in% w

now r gives me the presence or absence of elements in z that are in w but I am 
interested in getting the number of times each element in z appears (or doesn't 
appear)  in w. I want the dimension of my resulting vector to be the same as 
that of z. How do I do that?

 Thanks in advance
 Mintewab


Please access the attached hyperlink for an important electronic communications 
disclaimer: http://lse.ac.uk/emailDisclaimer
[[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.


Re: [R] counting matched elements in two vectors

2014-01-23 Thread Jeff Newmiller
Thank you for the reproducible example, but your description is missing a clear 
definition of what you want.

For example, if your desired output is 
result - c(rep(0,16),2,1,0,3,1,1,0,1,0,0,0,0,0,0,1,3,2,0,0,1,rep(0,10))

then one answer might be

as.vector(table(factor(w,levels=z)))

---
Jeff NewmillerThe .   .  Go Live...
DCN:jdnew...@dcn.davis.ca.usBasics: ##.#.   ##.#.  Live Go...
  Live:   OO#.. Dead: OO#..  Playing
Research Engineer (Solar/BatteriesO.O#.   #.O#.  with
/Software/Embedded Controllers)   .OO#.   .OO#.  rocks...1k
--- 
Sent from my phone. Please excuse my brevity.

m.beza...@lse.ac.uk wrote:
   Hi all,
I have the following reproducible example

z-c(-5:40)
w-c(11, 11, 12, 14, 14, 14, 15, 16, 18, 25, 26, 26, 26, 27, 27, 30)
 r-z %in% w

now r gives me the presence or absence of elements in z that are in w
but I am interested in getting the number of times each element in z
appears (or doesn't appear)  in w. I want the dimension of my resulting
vector to be the same as that of z. How do I do that?

 Thanks in advance
 Mintewab


Please access the attached hyperlink for an important electronic
communications disclaimer: http://lse.ac.uk/emailDisclaimer
   [[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.

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


Re: [R] counting matched elements in two vectors

2014-01-23 Thread Hervé Pagès

Hi Mintewab,

With the IRanges packages (from Bioconductor):

   library(IRanges)
   countMatches(z, w)
   [1] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2 1 0 3 1 1 0 1 0 0 0 0 0 0 1 3 
2 0 0 1 0 0

  [39] 0 0 0 0 0 0 0 0

To install the IRanges package:

  source(http://bioconductor.org/biocLite.R;)
  biocLite(IRanges)

Cheers,
H.


On 01/23/2014 07:43 AM, m.beza...@lse.ac.uk wrote:

Hi all,
I have the following reproducible example

z-c(-5:40)
w-c(11, 11, 12, 14, 14, 14, 15, 16, 18, 25, 26, 26, 26, 27, 27, 30)
  r-z %in% w

now r gives me the presence or absence of elements in z that are in w but I am 
interested in getting the number of times each element in z appears (or doesn't 
appear)  in w. I want the dimension of my resulting vector to be the same as 
that of z. How do I do that?

  Thanks in advance
  Mintewab


Please access the attached hyperlink for an important electronic communications 
disclaimer: http://lse.ac.uk/emailDisclaimer
[[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.



--
Hervé Pagès

Program in Computational Biology
Division of Public Health Sciences
Fred Hutchinson Cancer Research Center
1100 Fairview Ave. N, M1-B514
P.O. Box 19024
Seattle, WA 98109-1024

E-mail: hpa...@fhcrc.org
Phone:  (206) 667-5791
Fax:(206) 667-1319

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


Re: [R] counting matched elements in two vectors

2014-01-23 Thread Peter Langfelder
Here's a solution:

# This gives a vector of counts (if z is a data frame, first convert
it to a matrix)
res = sapply(as.vector(z), function(x) sum(w==x))
# This copies the dimensions of the variable 'z' to 'res':
dim(res) = dim(z)

Peter

On Thu, Jan 23, 2014 at 7:43 AM,  m.beza...@lse.ac.uk wrote:
Hi all,
 I have the following reproducible example

 z-c(-5:40)
 w-c(11, 11, 12, 14, 14, 14, 15, 16, 18, 25, 26, 26, 26, 27, 27, 30)
  r-z %in% w

 now r gives me the presence or absence of elements in z that are in w but I 
 am interested in getting the number of times each element in z appears (or 
 doesn't appear)  in w. I want the dimension of my resulting vector to be the 
 same as that of z. How do I do that?

  Thanks in advance
  Mintewab


 Please access the attached hyperlink for an important electronic 
 communications disclaimer: http://lse.ac.uk/emailDisclaimer
 [[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.

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


Re: [R] counting matched elements in two vectors

2014-01-23 Thread Hervé Pagès

On 01/23/2014 04:49 PM, Hervé Pagès wrote:

Hi Mintewab,

With the IRanges packages (from Bioconductor):

library(IRanges)
countMatches(z, w)
[1] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2 1 0 3 1 1 0 1 0 0 0 0 0 0 1 3
2 0 0 1 0 0
   [39] 0 0 0 0 0 0 0 0


And if you don't want to depend on IRanges for such a simple operation,
here how countMatches() is implemented:

  countMatches - function(x, table)
  {
  table2 - match(table, x)
  x2 - match(x, x)
  tabulate(table2, nbins=length(x))[x2]
  }

Cheers,
H.



To install the IRanges package:

   source(http://bioconductor.org/biocLite.R;)
   biocLite(IRanges)

Cheers,
H.


On 01/23/2014 07:43 AM, m.beza...@lse.ac.uk wrote:

Hi all,
I have the following reproducible example

z-c(-5:40)
w-c(11, 11, 12, 14, 14, 14, 15, 16, 18, 25, 26, 26, 26, 27, 27, 30)
  r-z %in% w

now r gives me the presence or absence of elements in z that are in w
but I am interested in getting the number of times each element in z
appears (or doesn't appear)  in w. I want the dimension of my
resulting vector to be the same as that of z. How do I do that?

  Thanks in advance
  Mintewab


Please access the attached hyperlink for an important electronic
communications disclaimer: http://lse.ac.uk/emailDisclaimer
[[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.





--
Hervé Pagès

Program in Computational Biology
Division of Public Health Sciences
Fred Hutchinson Cancer Research Center
1100 Fairview Ave. N, M1-B514
P.O. Box 19024
Seattle, WA 98109-1024

E-mail: hpa...@fhcrc.org
Phone:  (206) 667-5791
Fax:(206) 667-1319

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