[R] help: program efficiency

Mike Marchywka marchywka at hotmail.com
Thu Nov 25 18:34:36 CET 2010





----------------------------------------
> Date: Thu, 25 Nov 2010 06:49:19 -0800
> From: randomcz at gmail.com
> To: r-help at r-project.org
> Subject: [R] help: program efficiency
>
>
> hey guys,
>
> I am working on a function to make a duplicated value unique. For example,
> the original vector would be like : a = c(2,1,1,3,3,3,4)
> I'll like to transform it into:
> a.nodup = 2, 1.01, 1.02, 3.01, 3.02, 3.03, 4
> basically, find the duplicates and assign a unique value by adding a small
> amount and keep it in order.
> I come up with the following codes, but it runs slow if t is large. Is there
> a better way to do it?


I guess I'd just make a vector of uniform or even normal random numbers
and add to your input vector. This of course is not guaranteed and adds
to uniques  but you can test and repeat and it is probably closer
to what you want but I am only speculating on your objectives.


> nodup = function(t)
> {
> t.index=0
> t.dup=duplicated(t)
> for (i in 2:length(t))
> {
> if (t.dup[i]==T)
> t.index=t.index+0.01
> else t.index=0
> t[i]=t[i]+t.index
> }
> return(t)
> }
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/help-program-efficiency-tp3059079p3059079.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help at 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. 		 	   		  


More information about the R-help mailing list