[R] Number of replications of a term

2006-01-24 Thread Laetitia Marisa
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

Re: [R] Number of replications of a term

2006-01-24 Thread Barry Rowlingson
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

Re: [R] Number of replications of a term

2006-01-24 Thread Peter Dalgaard
Laetitia Marisa [EMAIL PROTECTED] writes: 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

Re: [R] Number of replications of a term

2006-01-24 Thread Chuck Cleland
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

Re: [R] Number of replications of a term

2006-01-24 Thread Liaw, Andy
This should work: ids - c( ID1, ID2, ID2, ID3, ID3,ID3, ID5) table(ids)[ids] ids ID1 ID2 ID2 ID3 ID3 ID3 ID5 1 2 2 3 3 3 1 Andy From: Laetitia Marisa Hello, Is there a simple and fast function that returns a vector of the number of replications for each object of a

Re: [R] Number of replications of a term

2006-01-24 Thread Gabor Grothendieck
Try this: ave(as.numeric(factor(ds)), ds, FUN = length) See ?ave for more info. On 1/24/06, Laetitia Marisa [EMAIL PROTECTED] 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

Re: [R] Number of replications of a term

2006-01-24 Thread Thomas Lumley
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

Re: [R] Number of replications of a term

2006-01-24 Thread jim holtman
?table ids - c( ID1, ID2, ID2, ID3, ID3,ID3, ID5) x - table(ids) x ids ID1 ID2 ID3 ID5 1 2 3 1 count - x[ids] # index using the names in the string count ids ID1 ID2 ID2 ID3 ID3 ID3 ID5 1 2 2 3 3 3 1 On 1/24/06, Laetitia Marisa [EMAIL PROTECTED] wrote: Hello,

Re: [R] Number of replications of a term

2006-01-24 Thread Thomas Lumley
Ah. It's a bit more complicated than just table(), because you want the result to be the same length. tt - table(id) tt[match(id,names(tt))] -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

Re: [R] Number of replications of a term

2006-01-24 Thread Laetitia Marisa
It is great! It takes now less than 1 second (with the table function (0.34'), 2 sec with the ave function (1.91') ) with my big data and only two lines of code ;). Thanks a lot every one. Regards, Laetitia. Laetitia Marisa [EMAIL PROTECTED] writes: Hello, Is there a simple and fast

Re: [R] Number of replications of a term

2006-01-24 Thread Gabor Grothendieck
Nice. I timed it and its much faster than mine too. On 1/24/06, Barry Rowlingson [EMAIL PROTECTED] wrote: 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

Re: [R] Number of replications of a term

2006-01-24 Thread Ray Brownrigg
There's an even faster one, which nobody seems to have mentioned yet: rep(l - rle(ids)$lengths, l) Timing on my 2.8GHz NetBSD system shows: length(ids) [1] 45150 # Gabor: system.time(for (i in 1:100) ave(as.numeric(factor(ids)), ids, FUN = length)) [1] 3.45 0.06 3.54 0.00 0.00 # Barry (and

Re: [R] Number of replications of a term

2006-01-24 Thread Gabor Grothendieck
Note that that assumes that all occurrences of a value are contiguous. On 1/24/06, Ray Brownrigg [EMAIL PROTECTED] wrote: There's an even faster one, which nobody seems to have mentioned yet: rep(l - rle(ids)$lengths, l) Timing on my 2.8GHz NetBSD system shows: length(ids) [1] 45150 #

Re: [R] Number of replications of a term

2006-01-24 Thread Thomas Lumley
On Wed, 25 Jan 2006, Ray Brownrigg wrote: There's an even faster one, which nobody seems to have mentioned yet: rep(l - rle(ids)$lengths, l) I considered this but it wasn't clear to me from the initial post that each ID occupied a contiguous section of the vector. Also, lazy evaluation