Hi Everyone, Can I suggest a couple of new `codes' methods. I know codes is a bit dodgy (at least, according to the comments in codes.factor), but for what it's worth I like to extract codes from data frames of factors, for which the following two functions (which operate recursively) are helpful: "codes.default" <- function(x) { if (is.list(x)) lapply(x, codes) else codes(factor(x)) } "codes.data.frame" <- function(x) { row.names <- attr(x, "row.names") data.frame(codes(as.list(x)), row.names = row.names) } #### Hence: lvl <- ordered(c("L", "H"), c("L", "H")) llist <- rep(list(lvl), 4) codes(llist) # list of codes expand.grid(llist) # data frame of factors codes(expand.grid(llist)) # data frame of codes The non-list default behaviour is for data frames that mix factors with other stuff: the usual codes(factor(1:10)) caveat (from the help file) still applies, of course! Note that using codes on an array will produce a vector as the dim attribute is lost, eg codes(matrix(1:12, 3, 4)) # shocking! but although I toyed with the idea of mapping matrices to data frames in the default function I thought in the end that this was making a presumption about the interpretation of the rows and columns that was not justified. After all, you could still do codes(as.data.frame(matrix(1:12, 3, 4))) if the presumption *was* justified. Cheers, Jonathan. Jonathan Rougier Science Laboratories Department of Mathematical Sciences South Road University of Durham Durham DH1 3LE "[B]egin upon the precept ... that the things we see are to be weighed in the scale with what we know" (Meredith, 1879, The Egoist) -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-devel mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-devel-request@stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Jonathan Rougier <J.C.Rougier@durham.ac.uk> writes:> Can I suggest a couple of new `codes' methods. I know codes is a bit > dodgy (at least, according to the comments in codes.factor), but for what > it's worth I like to extract codes from data frames of factors, for which > the following two functions (which operate recursively) are helpful: > > "codes.default" <- > function(x) > { > if (is.list(x)) > lapply(x, codes) > else > codes(factor(x)) > } > > "codes.data.frame" <- > function(x) > { > row.names <- attr(x, "row.names") > data.frame(codes(as.list(x)), row.names = row.names) > }Looks quite nice. Two things have me worried, though: 1) do we really want to convert anything to factors? (try factor(rnorm(10000))) 2) if a dataframe contains a matrix, it gets converted to something with incompatible lengths -- O__ ---- Peter Dalgaard Blegdamsvej 3 c/ /'_ --- Dept. of Biostatistics 2200 Cph. N (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard@biostat.ku.dk) FAX: (+45) 35327907 -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-devel mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-devel-request@stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Hi Peter, On 4 Nov 1999, Peter Dalgaard BSA wrote:> Looks quite nice. Two things have me worried, though: > > 1) do we really want to convert anything to factors? (try factor(rnorm(10000)))I agree that this would be undesirable, but not necessarily wrong!> 2) if a dataframe contains a matrix, it gets converted to something > with incompatible lengthsThis was part of my motivation for including a matrix case in the default behaviour, but in the end I thought that the error message might be justified! But the alternative would be ... else if (is.matrix(x)) apply(t(x), 1, codes) else codes(factor(x)) but, as I mentioned before, this makes an unwarranted assumption about the interpretation of the rows and columns of x. Cheers, Jonathan. Jonathan Rougier Science Laboratories Department of Mathematical Sciences South Road University of Durham Durham DH1 3LE "[B]egin upon the precept ... that the things we see are to be weighed in the scale with what we know" (Meredith, 1879, The Egoist) -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-devel mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-devel-request@stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._