Debian Codegraph @

R/item-more-methods-Date.R

raw
1 setMethod("as.item","Date",function(x,...){
2
3 annotation <-new("annotation",NULL)
4 new("Date.item",
5 as.numeric(x),
6 measurement="date",
7 annotation=annotation)
8 })
9
10 setMethod("as.item","Date.item",as_item_item)
11
12 as.Date.numeric.item <- function(x,...)
13 as.Date(as.numeric(x),
14 origin=structure(0,class="Date"))
15
16 as.data.frame.Date.item <- function (x, row.names = NULL, optional = FALSE, ...)
17 {
18 nrows <- length(x)
19 nm <- paste(deparse(substitute(x), width.cutoff = 500), collapse = " ")
20 if (is.null(row.names)) {
21 if (nrows == 0L)
22 row.names <- character(0L)
23 else row.names <- .set_row_names(nrows)
24 }
25
26 x <- as.Date.numeric.item(x)
27
28 value <- list(x)
29 if (!optional)
30 names(value) <- nm
31 attr(value, "row.names") <- row.names
32 class(value) <- "data.frame"
33 value
34 }
35
36 str.Date.item <- function(object,give.head=TRUE,width=getOption("width"),...){
37 if(give.head){
38 hdr <- " Date item"
39 cat(hdr,"")
40 }
41 str(as.Date.numeric.item(object),give.head=FALSE,width=width,...)
42 }
43
44
45 format.Date.item <- function(x,...){
46 format(as.Date.numeric.item(x),...)
47 }
48 #setMethod("format","Date.item",format.Date.item)
49
50 print.Date.item <- function(x,
51 width=getOption("width"),
52 compress=FALSE,
53 ...,print.gap=NULL){
54 if(length(x)){
55 mkdots <- function(n) paste(rep(".",n),collapse="")
56 pg <- if(is.null(print.gap) || compress) 1 else print.gap
57 l <- length(x)
58 if(compress) {
59 x <- x[seq_len(min(width,l))]
60 x <- format(x,...)
61 x <- trimws(x)
62 xw <- cumsum(nchar(x,"w")+1)
63 hdr <- paste(" [","1:",length(x),"]",sep="")
64 width <- width - nchar(hdr)
65 if(any(xw > width)){
66 dots <- mkdots(3)
67 width <- width - nchar(dots,"w") - 1
68 upto <- max(which(xw <= width))
69 x <- trimws(format(c(x[seq_len(upto)],dots)))
70 }
71 cat(hdr,x)
72 cat("\n")
73 }
74 else
75 print.default(
76 format(x,...),
77 quote=FALSE,print.gap=print.gap)
78 }
79 else
80 print(as.Date.numeric.item(x),print.gap=print.gap)
81 }
82
83 setMethod("show","Date.item",function(object){
84 cat("\nDate item",
85 if(length(description(object))) sQuote(description(object)) else NULL,
86 paste("(",
87 "length = ",length(object),
88 ")",sep=""),
89 "\n\n")
90 print.Date.item(object,width=getOption("width"),compress=TRUE)
91 })
92 setMethod("print","Date.item",print.Date.item)
93
94 setMethod("summary","Date.item",function(object,...,maxsum=100,digits=3){
95 ism <- is.missing(object)
96 xvalid <- as.Date.numeric.item(object[!ism])
97 summary(xvalid)
98 })
99
100 setMethod("codebookEntry","Date.item",function(x){
101 annotation <- annotation(x)
102 filter <- [email protected]
103 spec <- c(
104 "Storage mode:"=storage.mode(x),
105 "Measurement:"="Date"
106 )
107 if(length(filter)) spec <- c(spec,
108 switch(class(filter),
109 missing.values = c("Missing values:" = format(filter)),
110 valid.values = c("Valid values:" = format(filter)),
111 valid.range = c("Valid range:" = format(filter))
112 ))
113 ism <- is.missing(x)
114 isna <- is.na(x)
115
116 if(any(ism | isna)){
117 tab <- missNAtab(ism,isna)
118 if(length(weights)){
119 wtab <- missNAtab(ism,isna,weights)
120 tab <- collect(unweighted=tab,
121 weighted=tab)
122 }
123 else
124 tab <- array(tab,
125 dim=c(dim(tab),1),
126 dimnames=c(dimnames(tab),
127 list(NULL)))
128 attr(tab,"title") <- "Valid and missing values"
129 } else
130 tab <- integer(0)
131
132 x <- as.Date(x)
133 descr <- Descriptives(x)
134 # if(length(weights)){
135 # wdescr <- Descriptives(x,weights)
136 # descr <- collect(unweighted=format(descr),
137 # weighted=format(wdescr))
138 # }
139 # else
140 descr <- as.matrix(format(descr))
141
142 stats <- list(tab=tab,
143 descr=descr)
144 new("codebookEntry",
145 spec = spec,
146 stats = stats,
147 annotation = annotation
148 )
149 })
150
151 ## Methods for auxiliar functions
152
153 xtfrm.Date.item <- function(x) [email protected]
154 mtfrm.Date.item <- function(x) [email protected]
155
156 setMethod("as.character","Date.item",function(x,...)
157 format(as.Date.numeric.item(x))
158 )
159
160 unique.Date.item <- unique.item.vector
161