| 1 |
## labels ################################################################################ |
| 2 |
|
| 3 |
setMethod("initialize","value.labels",function(.Object,...){ |
| 4 |
args <- list(...) |
| 5 |
values <- args$values |
| 6 |
labels <- args[[1]] |
| 7 |
ii <- order(values) |
| 8 |
values <- values[ii] |
| 9 |
labels <- labels[ii] |
| 10 |
labels <- iconv(labels,from="",to="UTF-8") |
| 11 |
Encoding(labels) <- "UTF-8" |
| 12 |
[email protected] <- labels |
| 13 |
.Object@values <- values |
| 14 |
if(validObject(.Object)) return(.Object) |
| 15 |
}) |
| 16 |
|
| 17 |
setValidity("value.labels",function(object){ |
| 18 |
if(length([email protected])==length(object@values)){ |
| 19 |
if(length(unique([email protected]))<=length(unique(object@values))) TRUE |
| 20 |
else paste("More labels than labelled values:\n", |
| 21 |
" ",length(unique([email protected])),"unique labels\n", |
| 22 |
" ",length(unique(object@values)),"labelled values") |
| 23 |
} |
| 24 |
else paste("There are",length(object@values),"values, but", |
| 25 |
length([email protected]),"labels") |
| 26 |
}) |
| 27 |
|
| 28 |
setMethod("labels","NULL",function(object,...)NULL) |
| 29 |
|
| 30 |
setMethod("labels","item",function(object,...)[email protected]) |
| 31 |
setMethod("labels<-",signature(x="item",value="ANY"),function(x,value){ |
| 32 |
[email protected]<-as(value,"value.labels") |
| 33 |
x |
| 34 |
}) |
| 35 |
|
| 36 |
setMethod("labels<-",signature(x="ANY",value="NULL"),function(x,value) x) |
| 37 |
|
| 38 |
setMethod("labels<-",signature(x="item",value="NULL"),function(x,value){ |
| 39 |
[email protected]<-NULL |
| 40 |
x |
| 41 |
}) |
| 42 |
|
| 43 |
setMethod("labels<-",signature(x="vector",value="NULL"),function(x,value) x) |
| 44 |
|
| 45 |
setMethod("labels<-",signature(x="vector",value="ANY"),function(x,value)as.item(x, |
| 46 |
labels=as(value,"value.labels"))) |
| 47 |
|
| 48 |
format.value.labels <- function(x,...){ |
| 49 |
paste(format(x@values,justify="right"),format(sQuote([email protected]),justify="left")) |
| 50 |
} |
| 51 |
print.value.labels <- function(x,...) writeLines(format.value.labels(x)) |
| 52 |
setMethod("show","value.labels",function(object){ |
| 53 |
writeLines(c("", |
| 54 |
" Values and labels:", |
| 55 |
"", |
| 56 |
paste(" ",format.value.labels(object)), |
| 57 |
"")) |
| 58 |
}) |
| 59 |
|
| 60 |
setMethod("[",signature(x="value.labels",i="numeric",j="missing",drop="missing"), |
| 61 |
function(x,i) new("value.labels",[email protected][i], values=x@values[i]) |
| 62 |
) |
| 63 |
|
| 64 |
setMethod("[",signature(x="value.labels",i="logical",j="missing",drop="missing"), |
| 65 |
function(x,i) |
| 66 |
new("value.labels",[email protected][i], values=x@values[i])) |
| 67 |
|
| 68 |
setAs(from="numeric",to="value.labels",function(from,to){ |
| 69 |
if(length(names(from))){ |
| 70 |
if(any(dups <- duplicated(names(from)))) |
| 71 |
warning("Duplicate labels ", |
| 72 |
paste(sQuote(unique(names(from)[dups])),collapse=" "), |
| 73 |
call.=FALSE) |
| 74 |
if(any(duplicated(from))) warning("Duplicate values",call.=FALSE) |
| 75 |
new("value.labels",names(from),values=unname(from)) |
| 76 |
} |
| 77 |
else new("value.labels",character(0),values=logical(0)) |
| 78 |
}) |
| 79 |
|
| 80 |
setAs(from="character",to="value.labels",function(from,to){ |
| 81 |
if(length(names(from))){ |
| 82 |
if(any(dups <- duplicated(names(from)))) |
| 83 |
warning("Duplicate labels", |
| 84 |
paste(sQuote(unique(names(from)[dups])),collapse=" "), |
| 85 |
call.=FALSE) |
| 86 |
if(any(duplicated(from))) warning("Duplicate values",call.=FALSE) |
| 87 |
new("value.labels",names(from),values=unname(from)) |
| 88 |
} |
| 89 |
else new("value.labels",character(0),values=logical(0)) |
| 90 |
}) |
| 91 |
|
| 92 |
|
| 93 |
setAs(from="value.labels",to="numeric",function(from,to) |
| 94 |
structure(as.vector(from@values),[email protected]) |
| 95 |
) |
| 96 |
|
| 97 |
setAs(from="value.labels",to="character",function(from,to) |
| 98 |
[email protected] |
| 99 |
) |
| 100 |
|
| 101 |
|
| 102 |
setMethod("as.vector","value.labels",function(x,mode="any") |
| 103 |
structure( |
| 104 |
as.vector(x@values,mode=mode), |
| 105 |
[email protected] |
| 106 |
) |
| 107 |
) |
| 108 |
|
| 109 |
|
| 110 |
setMethod("Arith",signature(e1="value.labels",e2="ANY"), |
| 111 |
function(e1,e2){ |
| 112 |
if(!is(e2,"value.labels")) |
| 113 |
e2 <- as(e2,"value.labels") |
| 114 |
vals <- e1@values |
| 115 |
labs <- [email protected] |
| 116 |
newvals <- e2@values |
| 117 |
newlabs <- [email protected] |
| 118 |
upd.vals <- intersect(vals,newvals) |
| 119 |
upd.labs <- newlabs[newvals %in% upd.vals] |
| 120 |
tmp <- setdiff(newvals,upd.vals) |
| 121 |
newlabs <- newlabs[match(tmp,newvals,nomatch=0L)] |
| 122 |
newvals <- tmp |
| 123 |
if(.Generic == "+"){ |
| 124 |
labs <- c(labs[vals %nin% upd.vals],upd.labs,newlabs) |
| 125 |
vals <- c(vals[vals %nin% upd.vals],upd.vals,newvals) |
| 126 |
ii <- order(vals) |
| 127 |
vals <- vals[ii] |
| 128 |
labs <- labs[ii] |
| 129 |
} |
| 130 |
else if(.Generic == "-"){ |
| 131 |
labs <- labs[vals %nin% upd.vals] |
| 132 |
vals <- vals[vals %nin% upd.vals] |
| 133 |
ii <- order(vals) |
| 134 |
vals <- vals[ii] |
| 135 |
labs <- labs[ii] |
| 136 |
} |
| 137 |
else stop("unsupported operator ",dQuote(.Generic)) |
| 138 |
new("value.labels",labs, |
| 139 |
values = vals) |
| 140 |
}) |
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
setMethod("has.value.labels","ANY",function(x)FALSE) |
| 145 |
setMethod("has.value.labels","item",function(x)length([email protected])>0) |
| 146 |
|
| 147 |
setMethod("is.labelled",signature(x="item.vector"),function(x){ |
| 148 |
if(!length([email protected])) return(logical(length(x))) |
| 149 |
cl <- [email protected] |
| 150 |
x %in% cl@values |
| 151 |
}) |
| 152 |
|
| 153 |
add_value_labels <- function(x,v){ |
| 154 |
if(length(labels(x))) |
| 155 |
labels(x) <- labels(x) + v |
| 156 |
else |
| 157 |
labels(x) <- v |
| 158 |
x |
| 159 |
} |