[R-gregmisc-commits] r2069 - pkg/gtools/R

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Thu Oct 15 23:15:58 CEST 2015


Author: warnes
Date: 2015-10-15 23:15:57 +0200 (Thu, 15 Oct 2015)
New Revision: 2069

Modified:
   pkg/gtools/R/smartbind.R
Log:
smartbind() was not properly handling columsn that were numeric on one df and character in the other and other similar ctype conflicts.  Fixed.



Modified: pkg/gtools/R/smartbind.R
===================================================================
--- pkg/gtools/R/smartbind.R	2015-10-15 20:46:52 UTC (rev 2068)
+++ pkg/gtools/R/smartbind.R	2015-10-15 21:15:57 UTC (rev 2069)
@@ -11,14 +11,14 @@
       }
 
     defaultNames <- seq.int(length(data))
-  
+
     if(is.null(names(data)))
       names(data) <- defaultNames
 
     emptyNames <- names(data)==""
     if (any(emptyNames) )
       names(data)[emptyNames] <- defaultNames[emptyNames]
-  
+
     data <- lapply(data,
                    function(x)
                    if(is.matrix(x) || is.data.frame(x))
@@ -95,7 +95,31 @@
                 retval[[col]] <- as.vector(rep(fill,nrows), mode=newclass)
               }
 
-            mode <- class(retval[[col]])
+            ## Handle case when current and previous native types differ
+            oldclass <- class(retval[[col]])
+
+            if(oldclass != newclass)
+            {
+              # handle conversions in case of conflicts
+              #   numeric vs integer --> numeric
+              #   complex vs numeric or integer --> complex
+              #   anything else:  --> character
+              if(oldclass %in% c("integer", "numeric") && newclass %in% c("integer", "numeric") )
+                class(retval[[col]]) <- mode <- "numeric"
+              else if(oldclass=="complex" && newclass %in% c("integer", "numeric") )
+                class(retval[[col]]) <- mode <- "complex"
+              else if(oldclass %in% c("integer", "numeric") && newclass=="complex")
+                class(retval[[col]]) <- mode <- "complex"
+              else
+                {
+                  class(retval[[col]]) <- mode <- "character"
+                  warning("Column class mismatch for '", col, "'. ",
+                          "Converting column to class 'character'.")
+                }
+            }
+            else
+              mode <- oldclass
+
             if(mode=="character")
                 vals <- as.character(block[,col])
             else



More information about the R-gregmisc-commits mailing list