| 1 |
genTable <- function (formula, |
| 2 |
data=parent.frame(), |
| 3 |
subset=NULL, |
| 4 |
names=NULL, |
| 5 |
addFreq=TRUE, |
| 6 |
...){ |
| 7 |
|
| 8 |
m <- match.call(expand.dots = FALSE) |
| 9 |
dots <- m$... |
| 10 |
if(length(formula) < 3){ |
| 11 |
m[[1]] <- as.name("xtabs") |
| 12 |
m[c("names","addFreq")] <- NULL |
| 13 |
m$... <- NULL |
| 14 |
return(eval(substitute(m),enclos=parent.frame())) |
| 15 |
} |
| 16 |
|
| 17 |
parent <- parent.frame() |
| 18 |
|
| 19 |
if(is.environment(data)){ |
| 20 |
data <- mget(all.vars(formula), |
| 21 |
envir=data, |
| 22 |
inherits=TRUE) |
| 23 |
} |
| 24 |
data <- as.data.frame(data) |
| 25 |
|
| 26 |
if(!missing(subset)){ |
| 27 |
subset <- eval(substitute(subset),data,parent) |
| 28 |
if(is.logical(subset)) |
| 29 |
subset <- subset & !is.na(subset) |
| 30 |
else stop("'subset' arg must be logical") |
| 31 |
data <- data[subset,,drop=FALSE] |
| 32 |
} |
| 33 |
|
| 34 |
|
| 35 |
by <- formula[-2] |
| 36 |
expr <- formula[[2]] |
| 37 |
|
| 38 |
if(length(expr)==1){ |
| 39 |
expr.c <- as.character(expr) |
| 40 |
if(is.factor(data[[expr.c]])) |
| 41 |
expr <- as.call(c(as.symbol("table"),expr)) |
| 42 |
else |
| 43 |
expr <- as.call(c(as.symbol("sum"),expr)) |
| 44 |
} |
| 45 |
if(addFreq){ |
| 46 |
if("Freq" %in% names(data) &&length(expr) > 1 && |
| 47 |
as.character(expr[[1]]) %in% c("table","Table","percent") && |
| 48 |
!("weights" %in% names(expr)) |
| 49 |
){ |
| 50 |
if(as.character(expr[[1]])=="table") |
| 51 |
expr[[1]] <- as.symbol("Table") |
| 52 |
if(length(expr) > 2){ |
| 53 |
if(!length(names(expr))) addFreq <- FALSE |
| 54 |
else if("weights" %in% names(expr)) |
| 55 |
addFreq <- FALSE |
| 56 |
} |
| 57 |
if(addFreq) |
| 58 |
expr$weights <- as.symbol("Freq") |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
|
| 63 |
if(deparse(formula[[3]])=="."){ |
| 64 |
vars <- setdiff(names(data),all.vars(expr)) |
| 65 |
by <- as.formula(paste("~",paste(vars,collapse="+"))) |
| 66 |
} |
| 67 |
if(length(dots)) expr <- as.call(c(as.list(expr),dots)) |
| 68 |
|
| 69 |
gdata <- Groups(data=data,by=by) |
| 70 |
|
| 71 |
wcall <- call("with",data=gdata,expr=expr) |
| 72 |
res <- eval(wcall,enclos=parent.frame()) |
| 73 |
spec <- attr(res,"spec") |
| 74 |
if(length(dim(res)) > length(spec) && !missing(names)) |
| 75 |
dimnames(res)[[1]] <- names |
| 76 |
res |
| 77 |
} |
| 78 |
|
| 79 |
Aggregate <- function (formula, |
| 80 |
data=parent.frame(), |
| 81 |
subset=NULL, |
| 82 |
names=NULL, |
| 83 |
addFreq=TRUE, |
| 84 |
drop=TRUE, |
| 85 |
as.vars=1, |
| 86 |
...){ |
| 87 |
|
| 88 |
m <- match.call() |
| 89 |
|
| 90 |
m[[1]] <- as.name("genTable") |
| 91 |
m$data <- force(data) |
| 92 |
res <- eval(m,enclos=parent.frame()) |
| 93 |
|
| 94 |
empty <- attr(res,"empty") |
| 95 |
spec <- attr(res,"spec") |
| 96 |
if(length(dim(res)) > length(spec)){ |
| 97 |
res <- to.data.frame(res,as.vars=as.vars) |
| 98 |
} |
| 99 |
else { |
| 100 |
res <- as.data.frame(res) |
| 101 |
if(!missing(names)) |
| 102 |
names(res)[3L] <- names |
| 103 |
else |
| 104 |
names(res)[3L] <- deparse(formula[[2]]) |
| 105 |
} |
| 106 |
if(drop && any(empty)){ |
| 107 |
nr <- nrow(res) |
| 108 |
ne <- length(empty) |
| 109 |
emtpy <- rep(as.vector(empty),each=nr/ne) |
| 110 |
res <- res[!empty,,drop=FALSE] |
| 111 |
} |
| 112 |
res |
| 113 |
} |