| 1 |
cases <- function(...,check.xor=c("warn","stop","ignore"), |
| 2 |
.default=NA,.complete=FALSE, |
| 3 |
check.na=c("warn","stop","ignore"), |
| 4 |
na.rm=TRUE){ |
| 5 |
subst <- match.call(expand.dots=FALSE)$... |
| 6 |
|
| 7 |
if(!missing(check.xor)) |
| 8 |
if(is.logical(check.xor)) |
| 9 |
check.xor <- ifelse(check.xor,"stop","ignore") |
| 10 |
else |
| 11 |
check.xor <- as.character(check.xor) |
| 12 |
if(!missing(check.na)) |
| 13 |
if(is.logical(check.na)) |
| 14 |
check.na <- ifelse(check.na,"stop","ignore") |
| 15 |
else |
| 16 |
check.na <- as.character(check.na) |
| 17 |
|
| 18 |
check.xor <- match.arg(check.xor) |
| 19 |
check.na <- match.arg(check.na) |
| 20 |
|
| 21 |
deflabels <- sapply(subst,deparse) |
| 22 |
if(length(subst)<2) stop("need at least two conditions") |
| 23 |
|
| 24 |
have.arrows <- sapply(subst,length) > 1 |
| 25 |
have.arrows[have.arrows] <- have.arrows[have.arrows] & sapply(sapply(subst[have.arrows],"[[",1),paste)=="<-" |
| 26 |
|
| 27 |
parent <- parent.frame() |
| 28 |
|
| 29 |
if(all(have.arrows)){ |
| 30 |
cond.names <- names(subst) |
| 31 |
|
| 32 |
conditions <- lapply(subst,"[[",3) |
| 33 |
values <- lapply(subst,"[[",2) |
| 34 |
conditions <- do.call(cbind,lapply(conditions,eval,envir=parent)) |
| 35 |
|
| 36 |
if(ncol(conditions)!=length(subst)) stop("at least one condition results in NULL") |
| 37 |
if(!is.logical(conditions)) stop("all conditions have to be logical") |
| 38 |
if(any(is.na(conditions)) && check.na!="ignore") { |
| 39 |
msg <- switch(check.na,warn=warning,stop=stop) |
| 40 |
msg("At least one logical condition results in missing values") |
| 41 |
} |
| 42 |
na.cond <- is.na(conditions) |
| 43 |
na.count <- rowSums(na.cond) |
| 44 |
any.na <- na.count > 0 |
| 45 |
all.na <- na.count == ncol(conditions) |
| 46 |
|
| 47 |
done <- rowSums(conditions, na.rm=TRUE) |
| 48 |
if((any(done > 1) || any(done >= 1 & any.na)) && check.xor!="ignore") { |
| 49 |
msg <- switch(check.xor,warn=warning,stop=stop) |
| 50 |
msg("Conditions are not mutually exclusive") |
| 51 |
} |
| 52 |
never <- colSums(conditions, na.rm=TRUE) == 0 |
| 53 |
if(any(never) && check.xor!="ignore"){ |
| 54 |
neverlab <- deflabels[never] |
| 55 |
if(length(neverlab)==1) |
| 56 |
warning("condition ",neverlab," is never satisfied") |
| 57 |
else |
| 58 |
warning("conditions ",paste(neverlab,collapse=", ")," are never satisfied") |
| 59 |
} |
| 60 |
|
| 61 |
values <- lapply(values,eval,envir=parent.frame(),enclos=parent.frame()) |
| 62 |
nrow <- unique(sapply(values,length)) |
| 63 |
if(length(nrow) > 1 || nrow != nrow(conditions)){ |
| 64 |
nrow <- nrow(conditions) |
| 65 |
values <- lapply(values,function(x){ |
| 66 |
tmp <- x |
| 67 |
length(tmp) <- nrow |
| 68 |
tmp[] <- x |
| 69 |
tmp |
| 70 |
}) |
| 71 |
} |
| 72 |
values <- do.call(cbind,values) |
| 73 |
na_ <- as.vector(NA,mode=storage.mode(values)) |
| 74 |
res <- vector(nrow(conditions),mode=storage.mode(values)) |
| 75 |
res[] <- na_ |
| 76 |
for(i in rev(1:ncol(conditions))){ |
| 77 |
cond.i <- which(conditions[,i]) |
| 78 |
res[cond.i] <- values[cond.i,i] |
| 79 |
} |
| 80 |
if(any(done == 0) && !is.na(.default)){ |
| 81 |
if(length(.default) > 1) warning("only first element of '.default' used") |
| 82 |
if(length(.default) < 1) stop("'.default' must have non-zero length") |
| 83 |
xvalue <- .default[1] |
| 84 |
xvalue <- as.vector(xvalue,mode=storage.mode(values)) |
| 85 |
res[done == 0 &!any.na] <- xvalue |
| 86 |
done <- done + 1 |
| 87 |
} |
| 88 |
if(!na.rm){ |
| 89 |
res[any.na] <- na_ |
| 90 |
} |
| 91 |
nNA <- sum(is.na(res)) |
| 92 |
if(nNA > 0) warning(nNA," NAs created") |
| 93 |
if(length(cond.names) && all(nzchar(cond.names))){ |
| 94 |
uq.values <- drop(unique(values)) |
| 95 |
if(length(uq.values)==length(cond.names)) |
| 96 |
labels(res) <- structure(unique(uq.values),names=cond.names) |
| 97 |
} |
| 98 |
res |
| 99 |
} |
| 100 |
else if(!any(have.arrows)) |
| 101 |
{ |
| 102 |
conditions <- cbind(...) |
| 103 |
if(ncol(conditions)!=length(subst)) stop("at least one condition results in NULL") |
| 104 |
if(!is.logical(conditions)) stop("all conditions have to be logical") |
| 105 |
if(any(is.na(conditions)) && check.na!="ignore") { |
| 106 |
msg <- switch(check.na,warn=warning,stop=stop) |
| 107 |
msg("At least one logical condition results in missing values") |
| 108 |
} |
| 109 |
na.cond <- is.na(conditions) |
| 110 |
na.count <- rowSums(na.cond) |
| 111 |
any.na <- na.count > 0 |
| 112 |
all.na <- na.count == ncol(conditions) |
| 113 |
|
| 114 |
codes <- 1:ncol(conditions) |
| 115 |
labels <- colnames(conditions) |
| 116 |
if(length(labels)) |
| 117 |
labels <- ifelse(nzchar(labels),labels,deflabels) |
| 118 |
else labels <- deflabels |
| 119 |
|
| 120 |
done <- rowSums(conditions,na.rm=TRUE) |
| 121 |
if((any(done > 1) || any(done >= 1 & any.na)) && check.xor!="ignore") { |
| 122 |
msg <- switch(check.xor,warn=warning,stop=stop) |
| 123 |
msg("conditions are not mutually exclusive") |
| 124 |
} |
| 125 |
|
| 126 |
never <- colSums(conditions, na.rm=TRUE) == 0 |
| 127 |
if(any(never) && check.xor!="ignore"){ |
| 128 |
neverlab <- deflabels[never] |
| 129 |
if(length(neverlab)==1) |
| 130 |
warning("condition ",neverlab," is never satisfied") |
| 131 |
else |
| 132 |
warning("conditions ",paste(neverlab,collapse=", ")," are never satisfied") |
| 133 |
} |
| 134 |
|
| 135 |
res <- integer(nrow(conditions)) |
| 136 |
res[] <- NA_integer_ |
| 137 |
|
| 138 |
for(i in rev(1:ncol(conditions))){ |
| 139 |
cond.i <- which(conditions[,i]) |
| 140 |
res[cond.i] <- i |
| 141 |
} |
| 142 |
if(any(done == 0 & !all.na) && .complete){ |
| 143 |
nms <- names(subst) |
| 144 |
if(length(nms)){ |
| 145 |
elevel <- paste(nms,collapse="|") |
| 146 |
} |
| 147 |
else { |
| 148 |
elevel <- sapply(subst,deparse) |
| 149 |
elevel <- paste(elevel,collapse=" | ") |
| 150 |
} |
| 151 |
elevel <- paste0("!(",elevel,")") |
| 152 |
xcode <- length(codes) + 1 |
| 153 |
res[done == 0 &!any.na] <- xcode |
| 154 |
codes <- c(codes,xcode) |
| 155 |
labels <- c(labels,elevel) |
| 156 |
done <- done + 1 |
| 157 |
} |
| 158 |
if(!na.rm) |
| 159 |
res[any.na] <- NA_integer_ |
| 160 |
nNA <- sum(is.na(res)) |
| 161 |
if(nNA > 0) warning(nNA," NAs created") |
| 162 |
factor(res,levels=codes,labels=labels) |
| 163 |
} |
| 164 |
else stop("inconsistent arguments to 'cases'") |
| 165 |
} |