Re: [R] Number of replications of a term

From: Thomas Lumley <tlumley_at_u.washington.edu>
Date: Wed 25 Jan 2006 - 05:01:45 EST

table()

         -thomas

On Tue, 24 Jan 2006, Laetitia Marisa wrote:

> Hello,
>
> Is there a simple and fast function that returns a vector of the number
> of replications for each object of a vector ?
> For example :
> I have a vector of IDs :
> ids <- c( "ID1", "ID2", "ID2", "ID3", "ID3","ID3", "ID5")
>
> I want the function returns the following vector where each term is the
> number of replicates for the given id :
> c( 1, 2, 2, 3,3,3,1 )
>
> Of course I have a vector of more than 40 000 ID and the function I
> wrote (it orders my data and checks on ID:Name of the data if the next
> term is the same as the previous one (see below) ) is really slow
> (30minutes for 44290 terms). But I don't have time by now to write a C
> function.
> Thanks a lot for your help,
>
> Laetitia.
>
>
>
> Here is the function I have written maybe I have done something not
> optimized :
>
> repVector <- function(obj){
>
> # order IDName
> ord <- gif.indexByIDName(obj)
> ordobj <- obj[ord,]
>
> nspots <- nrow(obj)
> # vector of spot replicates number
> spotrep <- rep(NA, nspots )
>
> # function to get ID:Name for a given spot
> spotidname <- function(ind){
> paste(ordobj$genes[ind, c("ID","Name") ], collapse=":")
> }
>
> spot <- 1
>
> while( spot < nspots ){
> i<-1
> while( spotidname(spot) == spotidname(spot + i) ){
>
> i <- i + 1
> }
>
> spotrep[spot : (spot + i-1)] <- i
> spot <- spot + i
> #cat("spot : ",spot,"\n")
> }
>
> obj$genes$spotrep <- spotrep[order(ord)]
>
> obj
>
> }
>
> ______________________________________________
> 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
>

Thomas Lumley			Assoc. Professor, Biostatistics
tlumley@u.washington.edu	University of Washington, Seattle

______________________________________________
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 Wed Jan 25 06:17:34 2006

This archive was generated by hypermail 2.1.8 : Wed 25 Jan 2006 - 08:13:51 EST