¿La mejor manera de crear consistencia genérica / de método para sort.data.frame?
Finalmente he decidido poner el método sort.data.frame que flota en Internet en un paquete R. Simplemente se solicita demasiado para dejarlo a un método de distribución ad hoc.
in embargo, está escrito con argumentos que lo hacen incompatible con la función de clasificación genérica:
sort(x,decreasing,...)
sort.data.frame(form,dat)
Si cambiosort.data.frame
para tomar la disminución como argumento como ensort.data.frame(form,decreasing,dat)
y descartar disminuyendo, entonces pierde su simplicidad porque siempre tendrá que especificardat=
y realmente no puede usar argumentos posicionales. Si lo agrego al final como ensort.data.frame(form,dat,decreasing)
, entonces el orden no coincide con la función genérica. Si espero que la disminución quede atrapada en los puntos `sort.data.frame (form, dat, ...), entonces, cuando use la coincidencia basada en la posición, creo que la función genérica asignará la segunda posición a la disminución y obtendrá descartado. ¿Cuál es la mejor manera de armonizar estas dos funciones?
La función completa es:
# Sort a data frame
sort.data.frame <- function(form,dat){
# Author: Kevin Wright
# http://tolstoy.newcastle.edu.au/R/help/04/09/4300.html
# Some ideas from Andy Liaw
# http://tolstoy.newcastle.edu.au/R/help/04/07/1076.html
# Use + for ascending, - for decending.
# Sorting is left to right in the formula
# Useage is either of the following:
# sort.data.frame(~Block-Variety,Oats)
# sort.data.frame(Oats,~-Variety+Block)
# If dat is the formula, then switch form and dat
if(inherits(dat,"formula")){
f=dat
dat=form
form=f
}
if(form[[1]] != "~") {
stop("Formula must be one-sided.")
}
# Make the formula into character and remove spaces
formc <- as.character(form[2])
formc <- gsub(" ","",formc)
# If the first character is not + or -, add +
if(!is.element(substring(formc,1,1),c("+","-"))) {
formc <- paste("+",formc,sep="")
}
# Extract the variables from the formula
vars <- unlist(strsplit(formc, "[\\+\\-]"))
vars <- vars[vars!=""] # Remove spurious "" terms
# Build a list of arguments to pass to "order" function
calllist <- list()
pos=1 # Position of + or -
for(i in 1:length(vars)){
varsign <- substring(formc,pos,pos)
pos <- pos+1+nchar(vars[i])
if(is.factor(dat[,vars[i]])){
if(varsign=="-")
calllist[[i]] <- -rank(dat[,vars[i]])
else
calllist[[i]] <- rank(dat[,vars[i]])
}
else {
if(varsign=="-")
calllist[[i]] <- -dat[,vars[i]]
else
calllist[[i]] <- dat[,vars[i]]
}
}
dat[do.call("order",calllist),]
}
Ejemplo
library(datasets)
sort.data.frame(~len+dose,ToothGrowth)