| 1 |
as_item_item <- function(x, |
| 2 |
labels=NULL, |
| 3 |
missing.values=NULL, |
| 4 |
valid.values=NULL, |
| 5 |
valid.range=NULL, |
| 6 |
value.filter=NULL, |
| 7 |
measurement=NULL, |
| 8 |
annotation=attr(x,"annotation"), |
| 9 |
... |
| 10 |
){ |
| 11 |
if(!missing(labels)) |
| 12 |
[email protected] <- if(length(labels)) as(labels,"value.labels") else NULL |
| 13 |
if(!(missing(missing.values) && missing(valid.values) && missing(valid.range) && missing(value.filter))){ |
| 14 |
mv <- length(missing.values) > 0 |
| 15 |
vv <- length(valid.values) > 0 |
| 16 |
vr <- length(valid.range) > 0 |
| 17 |
vf <- length(value.filter) > 0 |
| 18 |
if(mv + vv + vr + vf > 1) |
| 19 |
stop("to many value filter arguments") |
| 20 |
[email protected] <- if(mv) as(missing.values,"missing.values") |
| 21 |
else if(vv) as(valid.values,"valid.values") |
| 22 |
else if(vr) as(valid.range,"valid.range") |
| 23 |
else if(vf) { |
| 24 |
if(!is(value.filter,"value.filter")) |
| 25 |
stop("'value.filter' not of appropriate class") |
| 26 |
value.filter |
| 27 |
} |
| 28 |
else NULL |
| 29 |
} |
| 30 |
if(!missing(measurement)){ |
| 31 |
measurement <- if(length(measurement)) match.arg(measurement,c("nominal","ordinal","interval","ratio")) |
| 32 |
else if(length(labels) && !length(measurement)) "nominal" else "interval" |
| 33 |
measurement(x) <- measurement |
| 34 |
} |
| 35 |
if(!missing(annotation)){ |
| 36 |
annotation <- if(length(annotation)) |
| 37 |
new("annotation",structure(as.character(annotation),names=names(annotation))) |
| 38 |
else x@annotation |
| 39 |
annotation(x) <- annotation |
| 40 |
} |
| 41 |
x |
| 42 |
} |
| 43 |
|
| 44 |
setMethod("as.item","double.item",as_item_item) |
| 45 |
setMethod("as.item","integer.item",as_item_item) |
| 46 |
setMethod("as.item","character.item",as_item_item) |
| 47 |
|
| 48 |
setMethod("as.item","numeric",function(x, |
| 49 |
labels=NULL, |
| 50 |
missing.values=NULL, |
| 51 |
valid.values=NULL, |
| 52 |
valid.range=NULL, |
| 53 |
value.filter=NULL, |
| 54 |
measurement=NULL, |
| 55 |
annotation=attr(x,"annotation"), |
| 56 |
... |
| 57 |
){ |
| 58 |
value.labels <- if(length(labels)) as(labels,"value.labels") else NULL |
| 59 |
mv <- length(missing.values) > 0 |
| 60 |
vv <- length(valid.values) > 0 |
| 61 |
vr <- length(valid.range) > 0 |
| 62 |
vf <- length(value.filter) > 0 |
| 63 |
if(mv + vv + vr + vf > 1) |
| 64 |
stop("to many value filter arguments") |
| 65 |
value.filter <- if(mv) as(missing.values,"missing.values") |
| 66 |
else if(vv) as(valid.values,"valid.values") |
| 67 |
else if(vr) as(valid.range,"valid.range") |
| 68 |
else if(vf) { |
| 69 |
if(!is(value.filter,"value.filter")) |
| 70 |
stop("'value.filter' not of appropriate class") |
| 71 |
value.filter |
| 72 |
} |
| 73 |
measurement <- if(length(measurement)) match.arg(measurement,c("nominal","ordinal","interval","ratio")) |
| 74 |
else if(length(value.labels) && !length(measurement)) "nominal" else "interval" |
| 75 |
annotation <- new("annotation",structure(as.character(annotation),names=names(annotation))) |
| 76 |
label <- attr(x,"label") |
| 77 |
if("description" %nin% names(annotation) && length(label)) |
| 78 |
annotation["description"] <- label |
| 79 |
cl <- paste(storage.mode(x),"item",sep=".") |
| 80 |
new(cl, |
| 81 |
x, |
| 82 |
value.labels=value.labels, |
| 83 |
value.filter=value.filter, |
| 84 |
measurement=measurement, |
| 85 |
annotation=annotation |
| 86 |
) |
| 87 |
}) |
| 88 |
|
| 89 |
setMethod("as.item","logical",function(x,...) { |
| 90 |
y <- as.integer(x) |
| 91 |
annotation <- attr(x,"annotation") |
| 92 |
label <- attr(x,"label") |
| 93 |
if("description" %nin% names(annotation) && length(label)) |
| 94 |
annotation["description"] <- label |
| 95 |
attr(y,"annotation") <- annotation |
| 96 |
as.item(y,...) |
| 97 |
}) |
| 98 |
setMethod("as.item","factor",function(x,...){ |
| 99 |
y <- as.integer(x) |
| 100 |
annotation <- attr(x,"annotation") |
| 101 |
label <- attr(x,"label") |
| 102 |
if("description" %nin% names(annotation) && length(label)) |
| 103 |
annotation["description"] <- label |
| 104 |
attr(y,"annotation") <- annotation |
| 105 |
y <- as.item(y, |
| 106 |
labels=new("value.labels",levels(x),values=seq_along(levels(x))), |
| 107 |
measurement="nominal", |
| 108 |
...) |
| 109 |
attr(y,"contrasts") <- attr(x,"contrasts") |
| 110 |
y |
| 111 |
}) |
| 112 |
setMethod("as.item","ordered",function(x,...){ |
| 113 |
y <- as.integer(x) |
| 114 |
annotation <- attr(x,"annotation") |
| 115 |
label <- attr(x,"label") |
| 116 |
if("description" %nin% names(annotation) && length(label)) |
| 117 |
annotation["description"] <- label |
| 118 |
attr(y,"annotation") <- annotation |
| 119 |
y <- as.item(y, |
| 120 |
labels=new("value.labels",levels(x),values=seq_along(levels(x))), |
| 121 |
measurement="ordinal", |
| 122 |
...) |
| 123 |
attr(y,"contrasts") <- attr(x,"contrasts") |
| 124 |
y |
| 125 |
}) |
| 126 |
|
| 127 |
setMethod("as.item","character",function(x, |
| 128 |
labels=NULL, |
| 129 |
missing.values=NULL, |
| 130 |
valid.values=NULL, |
| 131 |
valid.range=NULL, |
| 132 |
value.filter=NULL, |
| 133 |
measurement=NULL, |
| 134 |
annotation=attr(x,"annotation"), |
| 135 |
... |
| 136 |
){ |
| 137 |
value.labels <- if(length(labels)) as(labels,"value.labels") else NULL |
| 138 |
mv <- length(missing.values) > 0 |
| 139 |
vv <- length(valid.values) > 0 |
| 140 |
vr <- length(valid.range) > 0 |
| 141 |
vf <- length(value.filter) > 0 |
| 142 |
if(mv + vv + vr + vf > 1) |
| 143 |
stop("to many value filter arguments") |
| 144 |
value.filter <- if(mv) as(missing.values,"missing.values") |
| 145 |
else if(vv) as(valid.values,"valid.values") |
| 146 |
else if(vr) as(valid.range,"valid.range") |
| 147 |
else if(vf) { |
| 148 |
if(!is(value.filter,"value.filter")) |
| 149 |
stop("'value.filter' not of appropriate class") |
| 150 |
value.filter |
| 151 |
} |
| 152 |
measurement <- if(length(measurement)) match.arg(measurement,c("nominal","ordinal")) |
| 153 |
else "nominal" |
| 154 |
annotation <- new("annotation",structure(as.character(annotation),names=names(annotation))) |
| 155 |
label <- attr(x,"label") |
| 156 |
if("description" %nin% names(annotation) && length(label)) |
| 157 |
annotation["description"] <- label |
| 158 |
new("character.item",x, |
| 159 |
value.labels=value.labels, |
| 160 |
value.filter=value.filter, |
| 161 |
measurement=measurement, |
| 162 |
annotation=annotation |
| 163 |
) |
| 164 |
}) |
| 165 |
|
| 166 |
setMethod("[",signature(x="item.vector",i="numeric",j="missing",drop="missing"),function(x,i){ |
| 167 |
structure(new(class(x),[email protected][i], |
| 168 |
value.labels=labels(x), |
| 169 |
value.filter=value.filter(x), |
| 170 |
measurement=measurement(x), |
| 171 |
annotation=annotation(x) |
| 172 |
),contrasts=attr(x,"contrasts")) |
| 173 |
}) |
| 174 |
|
| 175 |
setMethod("[",signature(x="item.vector",i="logical",j="missing",drop="missing"),function(x,i){ |
| 176 |
structure(new(class(x),[email protected][i], |
| 177 |
value.labels=labels(x), |
| 178 |
value.filter=value.filter(x), |
| 179 |
measurement=measurement(x), |
| 180 |
annotation=annotation(x) |
| 181 |
),contrasts=attr(x,"contrasts")) |
| 182 |
}) |
| 183 |
|
| 184 |
|
| 185 |
str.item.vector <- function(object,give.head=TRUE,width=getOption("width"),...){ |
| 186 |
if(give.head){ |
| 187 |
hdr <- switch(measurement(object), |
| 188 |
nominal=" Nmnl. item", |
| 189 |
ordinal=" Ordl. item", |
| 190 |
interval=" Itvl. item", |
| 191 |
ratio=" Rto. item" |
| 192 |
) |
| 193 |
vl <- labels(object) |
| 194 |
if(length(vl)){ |
| 195 |
vlab <- sQuote([email protected]) |
| 196 |
vlval <- vl@values |
| 197 |
vlab <- ifelse(nchar(vlab)>10,paste(substr(vlab,start=1,stop=7),"..'",sep=""), |
| 198 |
vlab) |
| 199 |
lbl.header <- paste("w/",length([email protected]),"labels for") |
| 200 |
hdr <- paste(hdr,lbl.header) |
| 201 |
lbl.list <- paste(vlval) |
| 202 |
lbl.h.w <- nchar(hdr,"w") +1 + cumsum(nchar(lbl.list,"w")+1) |
| 203 |
use <- lbl.h.w < width*.6 |
| 204 |
if(any(use)) { |
| 205 |
use <- min(3,max(which(use))) |
| 206 |
if(use < length(lbl.list)){ |
| 207 |
lbl.list <- c(lbl.list[1:use],"...") |
| 208 |
} |
| 209 |
lbl.list <- paste(lbl.list,collapse=",") |
| 210 |
hdr <- paste(hdr,lbl.list) |
| 211 |
} |
| 212 |
} |
| 213 |
vf <- [email protected] |
| 214 |
if(length(vf)){ |
| 215 |
filt.hdr <- switch(class(vf), |
| 216 |
missing.values="ms.v.", |
| 217 |
valid.values="vd.v.", |
| 218 |
valid.range="vd.r." |
| 219 |
) |
| 220 |
hdr <- paste(hdr,filt.hdr,sep=" + ") |
| 221 |
} |
| 222 |
cat(hdr,"") |
| 223 |
} |
| 224 |
str([email protected],give.head=give.head,width=width,...) |
| 225 |
} |
| 226 |
|
| 227 |
str.double.item <- str.item.vector |
| 228 |
str.integer.item <- str.item.vector |
| 229 |
str.character.item <- str.item.vector |
| 230 |
|
| 231 |
## unique ############################################################################### |
| 232 |
|
| 233 |
unique.item.vector <- function(x, incomparables = FALSE, ...){ |
| 234 |
y <- new(class(x),unique([email protected])) |
| 235 |
attributes(y) <- attributes(x) |
| 236 |
y |
| 237 |
} |
| 238 |
|
| 239 |
unique.double.item <- unique.item.vector |
| 240 |
unique.integer.item <- unique.item.vector |
| 241 |
unique.character.item <- unique.item.vector |
| 242 |
|
| 243 |
|
| 244 |
## unique ############################################################################### |
| 245 |
|
| 246 |
setMethod("unique","item.vector",function(x, incomparables = FALSE, ...) |
| 247 |
new(class(x), |
| 248 |
.Data=callNextMethod(), |
| 249 |
annotation=x@annotation, |
| 250 |
[email protected], |
| 251 |
[email protected], |
| 252 |
measurement=x@measurement |
| 253 |
) |
| 254 |
) |
| 255 |
|
| 256 |
## coercion ############################################################################# |
| 257 |
|
| 258 |
|
| 259 |
as.data.frame.character.item <- |
| 260 |
as.data.frame.double.item <- |
| 261 |
as.data.frame.integer.item <- function (x, row.names = NULL, optional = FALSE, ...) |
| 262 |
{ |
| 263 |
nrows <- length(x) |
| 264 |
nm <- paste(deparse(substitute(x), width.cutoff = 500), collapse = " ") |
| 265 |
if (is.null(row.names)) { |
| 266 |
if (nrows == 0L) |
| 267 |
row.names <- character(0L) |
| 268 |
else row.names <- .set_row_names(nrows) |
| 269 |
} |
| 270 |
|
| 271 |
x <- if(is.ordinal(x)) as.ordered(x) |
| 272 |
else if(is.nominal(x)) as.factor(x) |
| 273 |
else as.vector(x) |
| 274 |
|
| 275 |
value <- list(x) |
| 276 |
if (!optional) |
| 277 |
names(value) <- nm |
| 278 |
attr(value, "row.names") <- row.names |
| 279 |
class(value) <- "data.frame" |
| 280 |
value |
| 281 |
} |
| 282 |
|
| 283 |
setMethod("as.vector","item",function(x,mode = "any"){ |
| 284 |
ism <- is.missing(x) |
| 285 |
d <- description(x) |
| 286 |
x <- callNextMethod() |
| 287 |
if(mode=="any") mode <- storage.mode(x) |
| 288 |
x[ism] <- as.vector(NA,mode=mode) |
| 289 |
if(length(d)) |
| 290 |
attr(x,"label") <- d |
| 291 |
x |
| 292 |
}) |
| 293 |
|
| 294 |
setMethod("as.numeric","item",function(x,...) as.vector(x,mode="double")) |
| 295 |
setMethod("as.integer","item",function(x,...) as.vector(x,mode="integer")) |
| 296 |
|
| 297 |
setMethod("as.ordered","item.vector",function(x){ |
| 298 |
labels <- [email protected] |
| 299 |
if(length(labels)){ |
| 300 |
values <- labels@values |
| 301 |
labels <- [email protected] |
| 302 |
} |
| 303 |
else { |
| 304 |
values <- labels <- sort(unique([email protected])) |
| 305 |
} |
| 306 |
filter <- [email protected] |
| 307 |
use.levels <- if(length(filter)) is.valid2(values,filter) else TRUE |
| 308 |
values <- values[use.levels] |
| 309 |
labels <- labels[use.levels] |
| 310 |
if(any(duplicated(values))) stop("Duplicate values") |
| 311 |
if(any(duplicated(labels))) stop("Duplicate labels") |
| 312 |
f <- ordered([email protected],levels=values,labels=labels) |
| 313 |
if(length(attr(x,"contrasts"))) |
| 314 |
attr(f,"contrasts") <- contrasts(x) |
| 315 |
d <- description(x) |
| 316 |
if(length(d)) |
| 317 |
attr(f,"label") <- d |
| 318 |
f |
| 319 |
}) |
| 320 |
|
| 321 |
setMethod("as.factor","item.vector",function(x){ |
| 322 |
labels <- [email protected] |
| 323 |
if(length(labels)){ |
| 324 |
values <- labels@values |
| 325 |
labels <- [email protected] |
| 326 |
} |
| 327 |
else { |
| 328 |
values <- labels <- sort(unique([email protected])) |
| 329 |
} |
| 330 |
filter <- [email protected] |
| 331 |
use.levels <- if(length(filter)) is.valid2(values,filter) else TRUE |
| 332 |
values <- values[use.levels] |
| 333 |
labels <- labels[use.levels] |
| 334 |
if(any(duplicated(values))) stop("Duplicate values") |
| 335 |
if(any(duplicated(labels))) stop("Duplicate labels") |
| 336 |
f <- factor([email protected],levels=values,labels=labels) |
| 337 |
if(length(attr(x,"contrasts"))) |
| 338 |
contrasts(f) <- contrasts(x) |
| 339 |
d <- description(x) |
| 340 |
if(length(d)) |
| 341 |
attr(f,"label") <- d |
| 342 |
f |
| 343 |
}) |
| 344 |
|
| 345 |
setMethod("as.character","item.vector",function(x,use.labels=TRUE,include.missings=FALSE,...){ |
| 346 |
d <- description(x) |
| 347 |
ism <- is.missing(x) & !isTRUE(include.missings) |
| 348 |
if(use.labels && length(vl <- labels(x))){ |
| 349 |
i <- match([email protected],vl@values) |
| 350 |
y <- [email protected][i] |
| 351 |
y[is.na(y)] <- as.character([email protected][is.na(y)]) |
| 352 |
if(length(d)) |
| 353 |
attr(y,"label") <- d |
| 354 |
} |
| 355 |
else { |
| 356 |
y <- as.character([email protected]) |
| 357 |
if(length(d)) |
| 358 |
attr(y,"label") <- d |
| 359 |
} |
| 360 |
y[ism] <- as.vector(NA,mode="character") |
| 361 |
y |
| 362 |
}) |
| 363 |
|
| 364 |
|
| 365 |
## utilities ############################################################################# |
| 366 |
|
| 367 |
relabel_item <- function(x,...,gsub=FALSE,fixed=TRUE,warn=TRUE){ |
| 368 |
subst <- c(...) |
| 369 |
vl <- [email protected] |
| 370 |
vll <- [email protected] |
| 371 |
if(gsub){ |
| 372 |
for(i in 1:length(subst)){ |
| 373 |
vll <- gsub(names(subst[i]),subst[i],vll,fixed=fixed) |
| 374 |
} |
| 375 |
} |
| 376 |
else { |
| 377 |
i <- match(names(subst),vll) |
| 378 |
if(any(is.na(i))) { |
| 379 |
if(warn) warning("undefined label(s) selected") |
| 380 |
if(any(!is.na(i))) |
| 381 |
subst <- subst[!is.na(i)] |
| 382 |
i <- i[!is.na(i)] |
| 383 |
} |
| 384 |
vll[i] <- subst |
| 385 |
} |
| 386 |
[email protected] <- vll |
| 387 |
if(validObject(vl)) |
| 388 |
[email protected] <- vl |
| 389 |
return(x) |
| 390 |
} |
| 391 |
|
| 392 |
setMethod("relabel4","item",function(x,...)relabel_item(x,...)) |
| 393 |
|
| 394 |
|
| 395 |
setMethod("summary","item.vector",function(object,...,maxsum=100,digits=3) |
| 396 |
switch(measurement(object), |
| 397 |
nominal = smry.nominal.vector(x=object,...,maxsum=maxsum), |
| 398 |
ordinal = smry.ordinal.vector(x=object,...,maxsum=maxsum), |
| 399 |
interval = smry.interval.vector(x=object,...,maxsum=maxsum,digits=digits), |
| 400 |
ratio = smry.ratio.vector(x=object,...,maxsum=maxsum,digits=digits) |
| 401 |
) |
| 402 |
) |
| 403 |
|
| 404 |
smry.nominal.vector <- smry.ordinal.vector <- function(x,...,maxsum=100,digits=3){ |
| 405 |
ism <- is.missing(x) |
| 406 |
isna <- is.na(x) |
| 407 |
xvalid <- [email protected][!ism] |
| 408 |
uvalid <- sort(unique(xvalid)) |
| 409 |
tabvalid <- if(has.value.labels(x)) { |
| 410 |
vl <- [email protected] |
| 411 |
vv <- match(uvalid,vl@values) |
| 412 |
nms <- [email protected][vv] |
| 413 |
nms[is.na(nms)] <- "" |
| 414 |
structure(tabulate(match(xvalid,uvalid)),names=nms) |
| 415 |
} |
| 416 |
else structure(tabulate(match(xvalid,uvalid)),names=uvalid) |
| 417 |
tabmis <- if(any(ism)) { |
| 418 |
xmis <- [email protected][ism & !isna] |
| 419 |
umis <- sort(unique(xmis)) |
| 420 |
if(has.value.labels(x)) { |
| 421 |
vl <- [email protected] |
| 422 |
vv <- match(umis,vl@values) |
| 423 |
nms <- [email protected][vv] |
| 424 |
nms[is.na(nms)] <- "" |
| 425 |
structure(tabulate(match(xmis,umis)),names=paste("*",nms,sep="")) |
| 426 |
} |
| 427 |
else structure(tabulate(match(xmis,umis)),names=paste("*",umis,sep="")) |
| 428 |
} else integer(0) |
| 429 |
tabna <- if(any(isna)){ |
| 430 |
c("NAs"=sum(isna)) #' |
| 431 |
} else integer(0) |
| 432 |
if(length(tabvalid)+length(tabmis)+length(tabna)>maxsum){ |
| 433 |
if(length(tabmis)) tabmis <- c("missing values"=sum(tabmis)) |
| 434 |
maxsum <- maxsum - length(tabmis) - length(tabna) |
| 435 |
if(any(seq_along(tabvalid) > maxsum)){ |
| 436 |
maxsum <- maxsum - 1 |
| 437 |
over <- seq_along(tabvalid) > maxsum |
| 438 |
tabvalid <- c(tabvalid[!over],"(Other)"=sum(tabvalid[over])) |
| 439 |
} |
| 440 |
} |
| 441 |
as.table(c(tabvalid,tabmis,tabna)) |
| 442 |
} |
| 443 |
|
| 444 |
smry.interval.vector <- smry.ratio.vector <- function(x,...,maxsum=100,digits=3){ |
| 445 |
ism <- is.missing(x) |
| 446 |
isna <- is.na(x) |
| 447 |
xvalid <- [email protected][!ism] |
| 448 |
tabvalid <- summary.default(xvalid) |
| 449 |
tabmis <- if(any(ism)) { |
| 450 |
xmis <- [email protected][ism & !isna] |
| 451 |
c("Missings"=length(xmis)) |
| 452 |
} else integer(0) |
| 453 |
tabna <- if(any(isna)){ |
| 454 |
c("NAs"=sum(isna)) |
| 455 |
} else integer(0) |
| 456 |
# if(length(tabvalid)+length(tabmis)+length(tabna)>maxsum){ |
| 457 |
# if(length(tabmis)) tabmis <- c("missing values"=sum(tabmis)) |
| 458 |
# maxsum <- maxsum - length(tabmis) - length(tabna) |
| 459 |
# if(any(seq_along(tabvalid) > maxsum)){ |
| 460 |
# over <- seq_along(tabvalid) > maxsum |
| 461 |
# tabvalid <- tabvalid[!over] |
| 462 |
# } |
| 463 |
# } |
| 464 |
as.table(c(tabvalid,tabmis,tabna)) |
| 465 |
} |
| 466 |
|
| 467 |
format.item.vector <- function(x,use.labels=getOption("print.use.value.labels"),justify="right",...){ |
| 468 |
ism <- is.missing(x) & !is.na(x) |
| 469 |
if(use.labels && has.value.labels(x)) |
| 470 |
x <- as.character(x,use.labels=use.labels,include.missings=TRUE) |
| 471 |
x <- format.default(x,trim=TRUE,justify="none",...) |
| 472 |
x[ism] <- paste("*",x[ism],sep="") |
| 473 |
format(x,justify=justify,...) |
| 474 |
} |
| 475 |
#setMethod("format","item.vector",format.item.vector) |
| 476 |
|
| 477 |
print.item.vector <- function(x, |
| 478 |
use.labels=isTRUE(getOption("print.use.value.labels")), |
| 479 |
width=getOption("width"), |
| 480 |
compress=FALSE,...,print.gap=NULL){ |
| 481 |
if(length(x)){ |
| 482 |
mkdots <- function(n) paste(rep(".",n),collapse="") |
| 483 |
pg <- if(is.null(print.gap) || compress) 1 else print.gap |
| 484 |
l <- length(x) |
| 485 |
if(compress) { |
| 486 |
x <- x[seq_len(min(width,l))] |
| 487 |
x <- format(x,use.labels=use.labels,...) |
| 488 |
x <- trimws(x) |
| 489 |
xw <- cumsum(nchar(x,"w")+1) |
| 490 |
hdr <- paste(" [","1:",l,"]",sep="") |
| 491 |
width <- width - nchar(hdr) |
| 492 |
if(any(xw > width)){ |
| 493 |
dots <- mkdots(3) |
| 494 |
width <- width - nchar(dots,"w") - 1 |
| 495 |
upto <- max(which(xw <= width)) |
| 496 |
x <- trimws(format(c(x[seq_len(upto)],dots))) |
| 497 |
} |
| 498 |
cat(hdr,x) |
| 499 |
cat("\n") |
| 500 |
} |
| 501 |
else |
| 502 |
print.default( |
| 503 |
format(x,use.labels=use.labels,...), |
| 504 |
quote=FALSE,print.gap=print.gap) |
| 505 |
} |
| 506 |
else |
| 507 |
print(as.vector(x),print.gap=print.gap) |
| 508 |
} |
| 509 |
|
| 510 |
setMethod("show","item.vector",function(object){ |
| 511 |
cat("\nItem", |
| 512 |
if(length(description(object))) sQuote(description(object)) else NULL, |
| 513 |
paste("(", |
| 514 |
"measurement: ",measurement(object),", ", |
| 515 |
"type: ",storage.mode(object),", ", |
| 516 |
"length = ",length(object),")",sep=""), |
| 517 |
"\n\n") |
| 518 |
print.item.vector(object,width=getOption("width"),compress=TRUE) |
| 519 |
}) |
| 520 |
setMethod("print","item.vector",print.item.vector) |
| 521 |
|
| 522 |
setMethod("Compare",signature(e1="numeric.item",e2="character"), |
| 523 |
function(e1,e2){ |
| 524 |
if(length([email protected])) |
| 525 |
switch(.Generic, |
| 526 |
"=="=, |
| 527 |
"!="=, |
| 528 |
">"=, |
| 529 |
"<"=, |
| 530 |
">="=, |
| 531 |
"<="=Compare_lvc1(.Generic,e1,e2), |
| 532 |
stop("Comparison not supported") |
| 533 |
) |
| 534 |
else callNextMethod() |
| 535 |
}) |
| 536 |
setMethod("Compare",signature(e1="character",e2="numeric.item"), |
| 537 |
function(e1,e2){ |
| 538 |
if(length([email protected])) |
| 539 |
switch(.Generic, |
| 540 |
"=="=, |
| 541 |
"!="=Compare_lvc1(.Generic,e2,e1), |
| 542 |
">"=Compare_lvc1("<",e2,e1), |
| 543 |
"<"=Compare_lvc1(">",e2,e1), |
| 544 |
">="=Compare_lvc1("<=",e2,e1), |
| 545 |
"<="=Compare_lvc1(">=",e2,e1), |
| 546 |
callNextMethod() |
| 547 |
) |
| 548 |
else callNextMethod() |
| 549 |
}) |
| 550 |
setMethod("Arith",signature(e1="numeric",e2="numeric.item"), |
| 551 |
function(e1,e2){ |
| 552 |
e1 <- as.vector(e1) |
| 553 |
e2 <- as.vector(e2) |
| 554 |
callNextMethod() |
| 555 |
}) |
| 556 |
setMethod("Arith",signature(e1="numeric.item",e2="numeric"), |
| 557 |
function(e1,e2){ |
| 558 |
e1 <- as.vector(e1) |
| 559 |
e2 <- as.vector(e2) |
| 560 |
callNextMethod() |
| 561 |
}) |
| 562 |
setMethod("Math","numeric.item",function(x){ |
| 563 |
x <- as.vector(x) |
| 564 |
callNextMethod() |
| 565 |
}) |
| 566 |
setMethod("Math2","numeric.item",function(x,digits){ |
| 567 |
x <- as.vector(x) |
| 568 |
callNextMethod() |
| 569 |
}) |
| 570 |
setMethod("Summary","numeric.item",function(x,...,na.rm=FALSE){ |
| 571 |
x <- x[!is.missing(x)] |
| 572 |
callNextMethod() |
| 573 |
}) |
| 574 |
|
| 575 |
|
| 576 |
Compare_lvc <- function(.Generic,e1,e2){ |
| 577 |
if(is(e1,"character")) Compare_lvc1(.Generic,e2,e1) |
| 578 |
else Compare_lvc1(.Generic,e1,e2) |
| 579 |
} |
| 580 |
|
| 581 |
Compare_lvc1 <- function(.Generic,e1,e2){ |
| 582 |
vl <- labels(e1) |
| 583 |
e1 <- [email protected][match(e1,vl@values)] |
| 584 |
ans <- callGeneric(e1,e2) |
| 585 |
} |
| 586 |
|
| 587 |
setMethod("%in%",signature(x="numeric.item",table="character"),function(x,table){ |
| 588 |
vl <- labels(x) |
| 589 |
x <- [email protected][match(x,vl@values)] |
| 590 |
x %in% table |
| 591 |
}) |
| 592 |
|
| 593 |
## Methods for the auxiliary function for 'sort' and 'order' |
| 594 |
|
| 595 |
xtfrm.integer.item <- function(x) [email protected] |
| 596 |
xtfrm.numeric.item <- function(x) [email protected] |
| 597 |
xtfrm.double.item <- function(x) [email protected] |
| 598 |
xtfrm.character.item <- function(x) as.integer(as.factor([email protected])) |
| 599 |
|
| 600 |
## Methods for the auxiliary function for 'match' |
| 601 |
|
| 602 |
mtfrm.integer.item <- function(x) [email protected] |
| 603 |
mtfrm.numeric.item <- function(x) [email protected] |
| 604 |
mtfrm.double.item <- function(x) [email protected] |
| 605 |
mtfrm.character.item <- function(x) as.integer(as.factor([email protected])) |
| 606 |
|
| 607 |
## rep |
| 608 |
|
| 609 |
setMethod("rep","item.vector",function(x,...){ |
| 610 |
|
| 611 |
[email protected] <- rep([email protected],...) |
| 612 |
x |
| 613 |
}) |
| 614 |
|
| 615 |
|
| 616 |
|