[Rodbcext-commits] r30 - pkg/genutils/R

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Thu Aug 23 09:56:22 CEST 2012


Author: jaunario
Date: 2012-08-23 09:56:21 +0200 (Thu, 23 Aug 2012)
New Revision: 30

Modified:
   pkg/genutils/R/mathutils.r
Log:
Added function dominant returns the 1st value with the most number of observations in a numeric vector.

Modified: pkg/genutils/R/mathutils.r
===================================================================
--- pkg/genutils/R/mathutils.r	2012-08-07 07:31:43 UTC (rev 29)
+++ pkg/genutils/R/mathutils.r	2012-08-23 07:56:21 UTC (rev 30)
@@ -19,4 +19,15 @@
   
   return(get(lop)(x,minofrange) & get(uop)(x, maxofrange))
   
-}
\ No newline at end of file
+}
+
+dominant <- function(x, na.rm=TRUE){
+  if (!is.numeric(x)) stop("x should be numeric.")
+  if (na.rm==TRUE) x <- x[!is.na(x)]
+  
+  if (length(x)<1) result <- NA else {
+    frq <- table(x)
+    result <- as.numeric(names(frq)[which(frq==max(frq))[1]]) # in case of tie get the first value
+  }
+  return(result)
+}



More information about the Rodbcext-commits mailing list