Skip to content
Prev 132669 / 398506 Next

Finding overlaps in vector

Johannes Graumann <johannes_graumann at web.de> wrote in
news:fkinut$re4$1 at ger.gmane.org:
If you want to retain the original rownames, then try:
[1] 0.00 0.45 1.00 2.00 3.00 3.25 3.33 3.75 4.10 5.00 6.00 6.45 7.00 
7.10 8.00

#-----start cut-and-pastable-----
#this will "label" individual group membership
#diff(.) returns a vector that is smaller by one than its input
#so it needs to be augmented with c(1,fn(diff((.))

grp.v<-cbind(vector,(c(1,1+cumsum(as.numeric(diff(vector)>0.5)))))

#You can then tally up the counts in groups

tb<-table(grp.v[,2])
tb

#1 2 3 4 5 6 7 8 
#2 1 1 5 1 2 2 1 
# And apply the counts to the rows by doing a 
# "row count" lookup into tb[.]

grp.v<-cbind(grp.v,tb[grp.v[,2]])
grp.v

-----end cut and pastable------
  vector    
1   0.00 1 2
1   0.45 1 2
2   1.00 2 1
3   2.00 3 1
4   3.00 4 5
4   3.25 4 5
4   3.33 4 5
4   3.75 4 5
4   4.10 4 5
5   5.00 5 1
6   6.00 6 2
6   6.45 6 2
7   7.00 7 2
7   7.10 7 2
8   8.00 8 1

Further processing of the membership "label" might better be accomplished 
by converting the matrix to a dataframe, and then working with the 
membership "label" as a factor. If you only want to deal with the 
rownames and values of vector that have more than <x> values, that should 
be straightforward.