Hello! Does R core find the following pacth usefull - I created methods for levels for list and data.frame, which can be usefull to get a list of levels for entries in a list or columns in a data.frame. Patch is attached and shown bellow example # Example> tmp <- list() > tmp$a <- factor(letters[1:10]) > tmp$b <- factor(letters[5:14]) > tmp$c <- 1:10 > tmp1 <- as.data.frame(tmp) > tmp2 <- list() > tmp2$"1" <- tmp > tmp2$"2" <- tmp1 > str(tmp2)List of 2 $ 1:List of 3 ..$ a: Factor w/ 10 levels "a","b","c","d",..: 1 2 3 4 5 6 7 8 9 10 ..$ b: Factor w/ 10 levels "e","f","g","h",..: 1 2 3 4 5 6 7 8 9 10 ..$ c: int [1:10] 1 2 3 4 5 6 7 8 9 10 $ 2:`data.frame': 10 obs. of 3 variables: ..$ a: Factor w/ 10 levels "a","b","c","d",..: 1 2 3 4 5 6 7 8 9 10 ..$ b: Factor w/ 10 levels "e","f","g","h",..: 1 2 3 4 5 6 7 8 9 10 ..$ c: int [1:10] 1 2 3 4 5 6 7 8 9 10> levels(tmp2)$"1" $"1"$a [1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" $"1"$b [1] "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" $"2" $"2"$a [1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" $"2"$b [1] "e" "f" "g" "h" "i" "j" "k" "l" "m" "n"> levels(tmp2, drop = FALSE)$"1" $"1"$a [1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" $"1"$b [1] "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" $"1"$c NULL $"2" $"2"$a [1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" $"2"$b [1] "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" $"2"$c NULL ---------------------------------------------------------------------- $ svn diff factor.R Index: factor.R ==================================================================--- factor.R (revision 37559) +++ factor.R (working copy) @@ -25,7 +25,25 @@ ## Help old S users: category <- function(...) .Defunct() -levels <- function(x) attr(x, "levels") +levels <- function(x, ...) UseMethod("levels") + +levels.default <- function(x, ...) attr(x, "levels") + +levels.list <- function(x, drop = TRUE) +{ + tmp <- lapply(x, levels, drop = drop) + if (drop) { + tmp1 <- unlist(lapply(tmp, is.null)) + tmp <- tmp[!tmp1] + } + return(tmp) +} + +levels.data.frame <- function(x, ...) +{ + return(levels.list(x, ...)) +} + nlevels <- function(x) length(levels(x)) "levels<-" <- function(x, value) UseMethod("levels<-") -- Lep pozdrav / With regards, Gregor Gorjanc ---------------------------------------------------------------------- University of Ljubljana PhD student Biotechnical Faculty Zootechnical Department URI: http://www.bfro.uni-lj.si/MR/ggorjan Groblje 3 mail: gregor.gorjanc <at> bfro.uni-lj.si SI-1230 Domzale tel: +386 (0)1 72 17 861 Slovenia, Europe fax: +386 (0)1 72 17 888 ---------------------------------------------------------------------- "One must learn by doing the thing; for though you think you know it, you have no certainty until you try." Sophocles ~ 450 B.C. ---------------------------------------------------------------------- -------------- next part -------------- A non-text attachment was scrubbed... Name: factor.R.diff.gz Type: application/x-gzip Size: 361 bytes Desc: not available Url : https://stat.ethz.ch/pipermail/r-devel/attachments/20060320/522fdf5f/attachment.gz
oops, this does not pass R CMD check. I will have to read manuals a bit more. ... * checking S3 generic/method consistency ... WARNING levels: function(x, ...) levels.list: function(x, drop) levels: function(x, ...) levels.data.frame: function(x, drop) ... Anyway, I would like to ask what is the "opinion" about writing methods for classes as list and data.frame. Methods for this might not be as simple as for numeric, character, factor and it would be nice that there would be some guidelines for at least: - what should be the "general" output i.e. list or something else - I understand that it is hard to say in advance, but a common policy might not hurt - what to do if a method for a list or data.frame can not be applied to each entry/column> Hello! > > Does R core find the following pacth usefull - I created methods for > levels for list and data.frame, which can be usefull to get a list of > levels for entries in a list or columns in a data.frame. Patch is > attached and shown bellow example > > # Example >> tmp <- list() >> tmp$a <- factor(letters[1:10]) >> tmp$b <- factor(letters[5:14]) >> tmp$c <- 1:10 >> tmp1 <- as.data.frame(tmp) >> tmp2 <- list() >> tmp2$"1" <- tmp >> tmp2$"2" <- tmp1 >> str(tmp2) > List of 2 > $ 1:List of 3 > ..$ a: Factor w/ 10 levels "a","b","c","d",..: 1 2 3 4 5 6 7 8 9 10 > ..$ b: Factor w/ 10 levels "e","f","g","h",..: 1 2 3 4 5 6 7 8 9 10 > ..$ c: int [1:10] 1 2 3 4 5 6 7 8 9 10 > $ 2:`data.frame': 10 obs. of 3 variables: > ..$ a: Factor w/ 10 levels "a","b","c","d",..: 1 2 3 4 5 6 7 8 9 10 > ..$ b: Factor w/ 10 levels "e","f","g","h",..: 1 2 3 4 5 6 7 8 9 10 > ..$ c: int [1:10] 1 2 3 4 5 6 7 8 9 10 > >> levels(tmp2) > $"1" > $"1"$a > [1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" > > $"1"$b > [1] "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" > > > $"2" > $"2"$a > [1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" > > $"2"$b > [1] "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" > >> levels(tmp2, drop = FALSE) > $"1" > $"1"$a > [1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" > > $"1"$b > [1] "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" > > $"1"$c > NULL > > > $"2" > $"2"$a > [1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" > > $"2"$b > [1] "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" > > $"2"$c > NULL > > ---------------------------------------------------------------------- > > $ svn diff factor.R > Index: factor.R > ==================================================================> --- factor.R (revision 37559) > +++ factor.R (working copy) > @@ -25,7 +25,25 @@ > ## Help old S users: > category <- function(...) .Defunct() > > -levels <- function(x) attr(x, "levels") > +levels <- function(x, ...) UseMethod("levels") > + > +levels.default <- function(x, ...) attr(x, "levels") > + > +levels.list <- function(x, drop = TRUE) > +{ > + tmp <- lapply(x, levels, drop = drop) > + if (drop) { > + tmp1 <- unlist(lapply(tmp, is.null)) > + tmp <- tmp[!tmp1] > + } > + return(tmp) > +} > + > +levels.data.frame <- function(x, ...) > +{ > + return(levels.list(x, ...)) > +} > + > nlevels <- function(x) length(levels(x)) > > "levels<-" <- function(x, value) UseMethod("levels<-") >-- Lep pozdrav / With regards, Gregor Gorjanc ---------------------------------------------------------------------- University of Ljubljana PhD student Biotechnical Faculty Zootechnical Department URI: http://www.bfro.uni-lj.si/MR/ggorjan Groblje 3 mail: gregor.gorjanc <at> bfro.uni-lj.si SI-1230 Domzale tel: +386 (0)1 72 17 861 Slovenia, Europe fax: +386 (0)1 72 17 888 ---------------------------------------------------------------------- "One must learn by doing the thing; for though you think you know it, you have no certainty until you try." Sophocles ~ 450 B.C.
Martin Maechler wrote:>>>>>>"Gregor" == Gregor Gorjanc <gregor.gorjanc at gmail.com> >>>>>> on Tue, 21 Mar 2006 20:50:21 +0100 writes: > > > Gregor> Martin Maechler wrote: > >> Hi Gregor, > >> > >> before even considering methods for "list" and > >> "data.frame", can you explain why you think it is > >> important for levels() to become a generic function at > >> all? For me, levels belong to factors (or then to > >> contour plots, or co-plots ) but exactly because level is > >> a too generic word, it seems to me to be problematic as a > >> generic function. > >> > >> How would describe the purpose of the levels() generic? > >> > > Gregor> You are right. I was in situation where I wanted to > Gregor> get levels from all entries (factors) of a list and > Gregor> considered to write a method for this instead of > Gregor> always using lapply(). It went quite smoothly, if I > Gregor> skip R CMD check error. After that I began to think > Gregor> and post a question to R-devel. Perhaps levels is > Gregor> really to generic, however one can set levels also > Gregor> for non-factors. Although, I agree that this is of > Gregor> no use. I was also playing with levels<- and find it > Gregor> quite hard to do something general and usable for > Gregor> lists and data.frame. > > Gregor> If we put levels aside, is there any opinion/policy > Gregor> about list, data.frame methods? Just curiosity. > > I don't understand the question. Do you mean the question if > methods should be written for "list" or for "data.frame" (which > inherit from "list")?Yes.> But in any case, can you please take this back to R-devel? > It has started there, and people should be able to see the > continuation / conclusion. >Hmm, replay all to your mail does not include r-devel, weird. Done. -- Lep pozdrav / With regards, Gregor Gorjanc ---------------------------------------------------------------------- University of Ljubljana PhD student Biotechnical Faculty Zootechnical Department URI: http://www.bfro.uni-lj.si/MR/ggorjan Groblje 3 mail: gregor.gorjanc <at> bfro.uni-lj.si SI-1230 Domzale tel: +386 (0)1 72 17 861 Slovenia, Europe fax: +386 (0)1 72 17 888 ---------------------------------------------------------------------- "One must learn by doing the thing; for though you think you know it, you have no certainty until you try." Sophocles ~ 450 B.C.