# [R] Avoiding loops to detect number of coincidences

Sarah Goslee sarah.goslee at gmail.com
Tue Jul 12 16:26:36 CEST 2011

```Hi Trying,

It would be helpful if you provided reproducible examples. It would
also be polite to sign a name so that we have something by which to

On Tue, Jul 12, 2011 at 8:00 AM, Trying To learn again
<tryingtolearnagain at gmail.com> wrote:
> Hi all,
>
> I have this information on a file ht.txt, imagine it is a data frame without
> labels:
>
> 1 1 1 8 1 1 6 4 1 3 1 3 3
> And on other table called "pru.txt" I have sequences similar this
>
> 4 1 1 8 1 1 6 4 1 3 1 3 3
> 1 6 1 8 1 1 6 4 1 3 1 3 3
> 1 1 1 8 1 1 6 4 1 3 1 3 3
> 6 6 6 8 1 1 6 4 1 3 1 3 3
> I want to now how many positions are identical between each row
> in pru compared with ht.

I have no idea what you are trying to do with the loops below, but if
you are trying to count matches by row:

> a reproducible example
> dput(ht)
c(1, 1, 1, 8, 1, 1, 6, 4, 1, 3, 1, 3, 3)
> dput(pru)
structure(list(V1 = c(4L, 1L, 1L, 6L), V2 = c(1L, 6L, 1L, 6L),
V3 = c(1L, 1L, 1L, 6L), V4 = c(8L, 8L, 8L, 8L), V5 = c(1L,
1L, 1L, 1L), V6 = c(1L, 1L, 1L, 1L), V7 = c(6L, 6L, 6L, 6L
), V8 = c(4L, 4L, 4L, 4L), V9 = c(1L, 1L, 1L, 1L), V10 = c(3L,
3L, 3L, 3L), V11 = c(1L, 1L, 1L, 1L), V12 = c(3L, 3L, 3L,
3L), V13 = c(3L, 3L, 3L, 3L)), .Names = c("V1", "V2", "V3",
"V4", "V5", "V6", "V7", "V8", "V9", "V10", "V11", "V12", "V13"
), class = "data.frame", row.names = c(NA, -4L))
> # count the positional matches by row
> apply(pru, 1, function(x)sum(x == ht))
[1] 12 12 13 10

Sarah

> n and m are the col and row of pru (m is the same number in pru and ht)
>
> I tried this with loops
>
> n<-nrow(pru)
> m<-ncol(pru)
>
> dacc2<-mat.or.vec(n, m)
>
> for (g in 1:n){
> for (j in 1:m){
> if(pru[g,j]-ht[1,j]!=0) dacc2[g,j]=0 else {dacc2[g,j]=1}
> }
> }
>
> So when I have dacc2 I can filter this:
>
> dar2<-pru[colSums(dacc2)>2 & colSums(dacc2)<10,]
>
> There is some way to avoid loops?
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help