The type.convert() function coerces data columns/vectors to an appropriate type. This is very useful, not only when importing data from files, but also for common data manipulation tasks. I would like to suggest enhancing the type.convert() function so it can be used to directly to handle data frames, lists, matrices, and non-character vectors: type.convert <- function(x, na.strings = "NA", as.is = FALSE, dec = ".", numerals = c("allow.loss", "warn.loss", "no.loss")) { if(is.list(x)) { for(i in seq_len(length(x))) x[[i]] <- .External2(C_typeconvert, as.character(x[[i]]), na.strings, as.is, dec, match.arg(numerals)) } else if(is.matrix(x)) { mode(x) <- "character" x <- .External2(C_typeconvert, x, na.strings, as.is, dec, match.arg(numerals)) } else { x <- .External2(C_typeconvert, as.character(x), na.strings, as.is, dec, match.arg(numerals)) } x } To test the added functionality without R, the function environment can be defined as: environment(type.convert) <- environment(utils::type.convert) Example: class(999) class(type.convert(999)) auto <- type.convert(mtcars) str(mtcars) # all numeric str(auto) # mix of numeric and integer I'm hoping the Core Team agrees that this enhancement of type.convert() provides a very useful function for the R users and developers, at a minimal cost. All the best, Arni
Seems fine to me. Would you mind providing a patch? The posted code needs some clean up (hints: it should only call .External2() in one place, use seq_along(), recurse, etc). Also, it should probably be made an S3 generic.
Created attachment 2247 [details] NAMESPACE
Created attachment 2248 [details] NAMESPACE (diff)
Created attachment 2249 [details] readtable.R
Created attachment 2250 [details] readtable.R (diff)
Created attachment 2251 [details] type.convert.Rd
Created attachment 2252 [details] type.convert.Rd (diff)
Thank you for the positive response. I have made the suggested changes - to call .External2() in one place, use seq_along(), recurse, and write as S3 generic. Working from the current SVN repo (r72665) I have modified three files inside trunk/src/library/utils: man/type.convert.Rd R/readtable.R NAMESPACE The new functionality can be tested using example(type.convert) after rebuilding R. The updated help page could be linked to from other help pages, such as mode.Rd. All the best, Arni
Thanks. I was thinking that by default as.character() would be applied to any incoming object, except for arrays (is.array) which use storage.mode<-(). Please add an example for the matrix case. Also, please combine the patches into a single file to simplify things.
Created attachment 2253 [details] Patch to enhance utils::type.convert Thanks again for constructive comments. I have revised the type.convert function so as.character() is applied to any incoming object, except for arrays (is.array) which use storage.mode<-(). Added an example for the matrix case and "see also" crosslinks to the class() and storage.mode() help pages, as well as a link back from the storage.mode() help page. The single-file patch created with 'svn diff' is hopefully a more convenient format than earlier attachments. All the best, Arni
Thanks for this contribution. The extra man page cross-references are thoughtful. I'll try to commit this soon.
Thanks, committed.