| 1 |
|
| 2 |
# setMethod("as.POSIXct","numeric.item",function(x,...){ |
| 3 |
# as.POSIXct([email protected],...) |
| 4 |
# }) |
| 5 |
|
| 6 |
|
| 7 |
as.POSIXct.datetime.item <- function(x, tz = "", ...){ |
| 8 |
origin <- if(is.null(x@origin)) "1970-01-01" else x@origin |
| 9 |
if(missing(tz)) { |
| 10 |
|
| 11 |
tz <- if(is.character(x@tzone) && !nzchar(x@tzone)) x@tzone else "UTC" |
| 12 |
} |
| 13 |
as.POSIXct([email protected], |
| 14 |
tz=tz, |
| 15 |
origin=origin) |
| 16 |
} |
| 17 |
|
| 18 |
setMethod("as.item","POSIXct",function(x,...){ |
| 19 |
|
| 20 |
annotation <-new("annotation",NULL) |
| 21 |
new("datetime.item", |
| 22 |
as.numeric(x), |
| 23 |
tzone=attr(x,"tzone"), |
| 24 |
origin=NULL, |
| 25 |
value.labels=NULL, |
| 26 |
value.filter=NULL, |
| 27 |
measurement="date/time", |
| 28 |
annotation=annotation) |
| 29 |
}) |
| 30 |
|
| 31 |
setMethod("as.item","datetime.item",as_item_item) |
| 32 |
|
| 33 |
|
| 34 |
setMethod("[",signature(x="datetime.item",i="numeric",j="missing",drop="missing"),function(x,i){ |
| 35 |
new("datetime.item",[email protected][i], |
| 36 |
origin=x@origin, |
| 37 |
tzone=x@tzone, |
| 38 |
value.labels=labels(x), |
| 39 |
value.filter=value.filter(x), |
| 40 |
measurement=measurement(x), |
| 41 |
annotation=annotation(x) |
| 42 |
) |
| 43 |
}) |
| 44 |
|
| 45 |
setMethod("[",signature(x="datetime.item",i="logical",j="missing",drop="missing"),function(x,i){ |
| 46 |
new("datetime.item",[email protected][i], |
| 47 |
origin=x@origin, |
| 48 |
tzone=x@tzone, |
| 49 |
value.labels=labels(x), |
| 50 |
value.filter=value.filter(x), |
| 51 |
measurement=measurement(x), |
| 52 |
annotation=annotation(x) |
| 53 |
) |
| 54 |
}) |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
as.data.frame.datetime.item <- function (x, row.names = NULL, optional = FALSE, ...) |
| 59 |
{ |
| 60 |
nrows <- length(x) |
| 61 |
nm <- paste(deparse(substitute(x), width.cutoff = 500), collapse = " ") |
| 62 |
if (is.null(row.names)) { |
| 63 |
if (nrows == 0L) |
| 64 |
row.names <- character(0L) |
| 65 |
else row.names <- .set_row_names(nrows) |
| 66 |
} |
| 67 |
|
| 68 |
x <- as.POSIXct.datetime.item(x) |
| 69 |
|
| 70 |
value <- list(x) |
| 71 |
if (!optional) |
| 72 |
names(value) <- nm |
| 73 |
attr(value, "row.names") <- row.names |
| 74 |
class(value) <- "data.frame" |
| 75 |
value |
| 76 |
} |
| 77 |
|
| 78 |
str.datetime.item <- function(object,give.head=TRUE,width=getOption("width"),...){ |
| 79 |
if(give.head){ |
| 80 |
hdr <- " Date-time item" |
| 81 |
cat(hdr,"") |
| 82 |
} |
| 83 |
str(as.POSIXct.datetime.item(object),give.head=FALSE,width=width,...) |
| 84 |
} |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
format.datetime.item <- function(x,justify="right",format="",tz="",usetz=FALSE,...){ |
| 90 |
if (missing(tz) && !is.null(tzone <- x@tzone)) tz <- tzone |
| 91 |
if(!nzchar(tz)) tz <- "UTC" |
| 92 |
format(as.POSIXct.datetime.item(x),format=format,tz=tz,usetz=usetz,...) |
| 93 |
} |
| 94 |
#setMethod("format","datetime.item",format.datetime.item) |
| 95 |
|
| 96 |
print.datetime.item <- function(x, |
| 97 |
width=getOption("width"), |
| 98 |
compress=FALSE, |
| 99 |
usetz=TRUE,...,print.gap=NULL){ |
| 100 |
if(length(x)){ |
| 101 |
mkdots <- function(n) paste(rep(".",n),collapse="") |
| 102 |
pg <- if(is.null(print.gap) || compress) 1 else print.gap |
| 103 |
l <- length(x) |
| 104 |
if(compress) { |
| 105 |
x <- x[seq_len(min(width,l))] |
| 106 |
x <- format.datetime.item(x,usetz=usetz,...) |
| 107 |
x <- trimws(x) |
| 108 |
xw <- cumsum(nchar(x,"w")+1) |
| 109 |
hdr <- paste(" [","1:",length(x),"]",sep="") |
| 110 |
width <- width - nchar(hdr) |
| 111 |
if(any(xw > width)){ |
| 112 |
dots <- mkdots(3) |
| 113 |
width <- width - nchar(dots,"w") - 1 |
| 114 |
upto <- max(which(xw <= width)) |
| 115 |
x <- trimws(format(c(x[seq_len(upto)],dots))) |
| 116 |
} |
| 117 |
cat(hdr,x) |
| 118 |
cat("\n") |
| 119 |
} |
| 120 |
else |
| 121 |
print.default( |
| 122 |
format.datetime.item(x,usetz=usetz,...), |
| 123 |
quote=FALSE,print.gap=print.gap) |
| 124 |
} |
| 125 |
else |
| 126 |
print(as.POSIXct.datetime.item(x),print.gap=print.gap) |
| 127 |
} |
| 128 |
|
| 129 |
setMethod("show","datetime.item",function(object){ |
| 130 |
tzone <- if(length(object@tzone)) object@tzone else "UTC" |
| 131 |
cat("\nDate/time item", |
| 132 |
if(length(description(object))) sQuote(description(object)) else NULL, |
| 133 |
paste("(", |
| 134 |
"length = ",length(object), |
| 135 |
", time zone = ",tzone, |
| 136 |
")",sep=""), |
| 137 |
"\n\n") |
| 138 |
print.datetime.item(object,width=getOption("width"),compress=TRUE,usetz=FALSE) |
| 139 |
}) |
| 140 |
setMethod("print","datetime.item",print.datetime.item) |
| 141 |
|
| 142 |
|
| 143 |
setMethod("summary","datetime.item",function(object,...,maxsum=100,digits=3){ |
| 144 |
ism <- is.missing(object) |
| 145 |
xvalid <- as.POSIXct.datetime.item(object[!ism]) |
| 146 |
summary(xvalid) |
| 147 |
}) |
| 148 |
|
| 149 |
missNAtab <- function(ism,isna,weights=NULL){ |
| 150 |
if(!length(weights)) |
| 151 |
weights <- rep(1,length(isna)) |
| 152 |
isvalid <- !isna & !ism |
| 153 |
isomiss <- ism & !isna |
| 154 |
counts <- c( |
| 155 |
Valid = sum(weights*isvalid), |
| 156 |
"NA" = sum(weights*isna), |
| 157 |
"Other missing" = sum(weights*isomiss), |
| 158 |
Total = sum(weights) |
| 159 |
) |
| 160 |
perc <- 100*counts/counts[length(counts)] |
| 161 |
perc[length(counts)] <- NA |
| 162 |
cbind(N=counts, |
| 163 |
Percent=perc) |
| 164 |
} |
| 165 |
|
| 166 |
|
| 167 |
setMethod("codebookEntry","datetime.item",function(x,weights,...){ |
| 168 |
annotation <- annotation(x) |
| 169 |
filter <- [email protected] |
| 170 |
spec <- c( |
| 171 |
"Storage mode:"=storage.mode(x), |
| 172 |
"Measurement:"="Date/time", |
| 173 |
"Origin:"=x@origin |
| 174 |
) |
| 175 |
if(length(filter)) spec <- c(spec, |
| 176 |
switch(class(filter), |
| 177 |
missing.values = c("Missing values:" = format(filter)), |
| 178 |
valid.values = c("Valid values:" = format(filter)), |
| 179 |
valid.range = c("Valid range:" = format(filter)) |
| 180 |
)) |
| 181 |
ism <- is.missing(x) |
| 182 |
isna <- is.na(x) |
| 183 |
|
| 184 |
if(any(ism | isna)){ |
| 185 |
tab <- missNAtab(ism,isna) |
| 186 |
if(length(weights)){ |
| 187 |
wtab <- missNAtab(ism,isna,weights) |
| 188 |
tab <- collect(unweighted=tab, |
| 189 |
weighted=tab) |
| 190 |
} |
| 191 |
else |
| 192 |
tab <- array(tab, |
| 193 |
dim=c(dim(tab),1), |
| 194 |
dimnames=c(dimnames(tab), |
| 195 |
list(NULL))) |
| 196 |
attr(tab,"title") <- "Valid and missing values" |
| 197 |
} else |
| 198 |
tab <- integer(0) |
| 199 |
|
| 200 |
x <- as.POSIXct(x) |
| 201 |
descr <- Descriptives(x) |
| 202 |
# if(length(weights)){ |
| 203 |
# wdescr <- Descriptives(x,weights) |
| 204 |
# descr <- collect(unweighted=format(descr), |
| 205 |
# weighted=format(wdescr)) |
| 206 |
# } |
| 207 |
# else |
| 208 |
descr <- as.matrix(format(descr)) |
| 209 |
|
| 210 |
stats <- list(tab=tab, |
| 211 |
descr=descr) |
| 212 |
new("codebookEntry", |
| 213 |
spec = spec, |
| 214 |
stats = stats, |
| 215 |
annotation = annotation |
| 216 |
) |
| 217 |
}) |
| 218 |
|
| 219 |
## Methods for auxiliar functions |
| 220 |
|
| 221 |
xtfrm.datetime.item <- function(x) [email protected] |
| 222 |
mtfrm.datetime.item <- function(x) [email protected] |
| 223 |
|
| 224 |
setMethod("as.character","datetime.item",function(x,...) |
| 225 |
format(as.POSIXct.datetime.item(x)) |
| 226 |
) |
| 227 |
|
| 228 |
unique.datetime.item <- unique.item.vector |