Debian Codegraph @

R/recode.R

raw
1 setGeneric("recode", function(x,...,
2 copy=getOption("recode_copy",identical(otherwise,"copy")),
3 otherwise=NA) standardGeneric("recode"))
4 setMethod("recode","item",function(x,...,
5 copy=getOption("recode_copy",identical(otherwise,"copy")),
6 otherwise=NA){
7 recodings <- match.call(expand.dots=FALSE)$...
8 recodings <- recodings[nzchar(sapply(recodings,paste,collapse=""))]
9 if(length(otherwise)!=1) stop("otherwise= argument must have length 1")
10
11 if(any(sapply(sapply(recodings,"[[",1),paste)!="<-"))
12 stop("invalid recoding request")
13 if(!length(recodings)) return(x)
14 newcodes <- lapply(recodings,"[[",2)
15 conditions <- lapply(recodings,"[[",3)
16 has.range <- paste(lapply(conditions,"[[",1)) == "range"
17 if(any(has.range)){
18 oldcodes <- conditions
19 has.min <- paste(lapply(conditions[has.range],"[[",2)) == "min"
20 has.max <- paste(lapply(conditions[has.range],"[[",3)) == "max"
21 if(any(has.min)){
22 min.list <- list(min=min(x))
23 conditions[has.range][has.min] <- lapply(
24 conditions[has.range][has.min],
25 function(x) do.call("substitute",list(x,min.list))
26 )
27 }
28 if(any(has.max)){
29 max.list <- list(max=max(x))
30 conditions[has.range][has.max] <- lapply(
31 conditions[has.range][has.max],
32 function(x) do.call("substitute",list(x,max.list))
33 )
34 }
35 }
36 else
37 oldcodes <- sapply(conditions,eval)
38 NA_recoded <- any(unlist(suppressWarnings(sapply(oldcodes,is.na))))
39
40 conditions[has.range] <- lapply(conditions[has.range],
41 function(x)
42 as.call(list(as.symbol("&"),
43 as.call(list(as.symbol("<="),x[[2]],quote([email protected]))),
44 as.call(list(as.symbol("<="),quote([email protected]),x[[3]]))
45 )
46 ))
47 conditions[!has.range] <- lapply(conditions[!has.range],
48 function(x)
49 as.call(list(as.symbol("%in%"),quote([email protected]),x))
50 )
51 torecode <- sapply(conditions,eval,envir=environment(),enclos=parent.frame())
52 if(!is.matrix(torecode)) torecode <- t(torecode)
53 newcodes <- sapply(newcodes,eval,parent.frame())
54 nevtrue <- colSums(torecode) == 0
55 if(any(nevtrue)){
56 nrecng <- paste(recodings[nevtrue])
57 if(length(nrecng)>1)
58 warning("recodings ",paste(nrecng,collapse=", ")," have no consequences")
59 else
60 warning("recoding ",nrecng," has no consequences")
61 }
62 ambiguous <- rowSums(torecode) > 1
63 if(any(ambiguous))
64 stop("recoding request is ambiguous")
65 #y <- if(is.character(newcodes)) as.character(x) else x
66 y <- x
67 labels(y) <- NULL
68 if(is.character(newcodes) && is.numeric(y)) {
69 newcodes <- structure(
70 seq_along(newcodes),
71 names=newcodes
72 )
73 }
74 #newcodes <- newcodes[!nevtrue]
75 #torecode <- torecode[,!nevtrue,drop=FALSE]
76 for(i in seq(along=newcodes)){
77 if(!nevtrue[i])
78 y[torecode[,i]] <- as.vector(newcodes[i],mode=storage.mode(y))
79 }
80 if(!copy){
81 recoded <- as.logical(rowSums(torecode))
82 tmp <- as.vector(otherwise,mode=storage.mode(y))
83 length(otherwise) <- length(y)
84 otherwise[] <- tmp
85 y[!recoded] <- otherwise[!recoded]
86 nNA <-sum(is.na(otherwise[!recoded])) - sum(is.na(x[!recoded]))
87 if(nNA > 0)
88 warning("recoding created ",nNA," NAs")
89 if(!NA_recoded)
90 y[is.na(x)] <- NA
91 }
92 newvlab <- newcodes[nzchar(names(newcodes))]
93 bijective <- FALSE
94 lab.y <- lab.x <- labels(x)
95 if(!is.list(oldcodes)){
96 # This happens only in one-to-one recodings
97 if(length(oldcodes) == length(newcodes) &&
98 all(oldcodes %in% newcodes)){
99 if(copy) bijective <- TRUE
100 else if(length(lab.x) && all(lab.x@values %in% oldcodes))
101 bijective <- TRUE
102 }
103 }
104 if(length(lab.x) && bijective && !length(names(newcodes))){
105 ii <- match(oldcodes,lab.x@values)
106 jj <- match(newcodes,lab.y@values)
107 [email protected][jj] <- [email protected][ii]
108 labels(y) <- lab.y
109 }
110 else if(length(lab.x) && copy){
111 lab.y.val <- lab.y@values
112 lab.oldcodes <- lapply(conditions,Substitute,list(x=lab.y.val))
113 lab.torecode <- sapply(lab.oldcodes,eval,envir=environment(),enclos=parent.frame())
114 for(i in seq(along=newcodes)){
115 lab.y.val[lab.torecode[,i]] <- as.vector(newcodes[i],mode=storage.mode(lab.y.val))
116 }
117 lab.y.val <- sort(unique(lab.y.val))
118 ii <- match(lab.y.val,lab.y@values)
119 lab.y.lab <- [email protected][ii]
120 labels(y) <- new("value.labels",lab.y.lab,values=lab.y.val)
121 if(length(newvlab))
122 labels(y) <- labels(y) + newvlab
123 }
124 else if(length(newvlab)){
125 if(length(names(otherwise)))
126 newvlab <- new("value.labels",names(newvlab),values=newvlab) + otherwise
127 labels(y) <- newvlab
128 if(measurement(y) %in% c("interval","ratio"))
129 measurement(y) <- "nominal"
130 }
131 # else if(length(newvlab)){
132 # if(identical(otherwise,"copy"))
133 # labels(y) <- labels(y) + newvlab
134 # else
135 # labels(y) <- newvlab
136 # }
137
138 return(y)
139 })
140
141 setMethod("recode","vector",function(x,...,
142 copy=getOption("recode_copy",identical(otherwise,"copy")),
143 otherwise=NA){
144 call <- match.call()
145
146 if(length(otherwise)!=1) stop("otherwise= argument must have length 1")
147
148 if("recodes" %in% names(call)
149 || is.character(call[[3]])
150 ) {
151 return(car_recode(var=x,...))
152 }
153 recodings <- match.call(expand.dots=FALSE)$...
154 if("to.factor" %in% names(recodings)
155 && is.logical(recodings[["to.factor"]])
156 ) {
157 warning("'to.factor' argument no longer in use")
158 recodings["to.factor"] <- NULL
159 }
160 recodings <- recodings[nzchar(sapply(recodings,paste,collapse=""))]
161 if(any(sapply(sapply(recodings,"[[",1),paste)!="<-"))
162 stop("invalid recoding request")
163
164 if(!length(recodings)) return(x)
165 newcodes <- lapply(recodings,"[[",2)
166 oldcodes <- lapply(recodings,"[[",3)
167 NA_recoded <- any(unlist(suppressWarnings(sapply(oldcodes,is.na))))
168 has.range <- paste(lapply(oldcodes,"[[",1)) == "range"
169 if(any(has.range)){
170 has.min <- paste(lapply(oldcodes[has.range],"[[",2)) == "min"
171 has.max <- paste(lapply(oldcodes[has.range],"[[",3)) == "max"
172 if(any(has.min)){
173 min.list <- list(min=min(x, na.rm=TRUE))
174 oldcodes[has.range][has.min] <- lapply(
175 oldcodes[has.range][has.min],
176 function(x) do.call("substitute",list(x,min.list))
177 )
178 }
179 if(any(has.max)){
180 max.list <- list(max=max(x, na.rm=TRUE))
181 oldcodes[has.range][has.max] <- lapply(
182 oldcodes[has.range][has.max],
183 function(x) do.call("substitute",list(x,max.list))
184 )
185 }
186 }
187 oldcodes[has.range] <- lapply(oldcodes[has.range],
188 function(x)
189 as.call(list(as.symbol("&"),
190 as.call(list(as.symbol("<="),x[[2]],as.symbol("x"))),
191 as.call(list(as.symbol("<="),as.symbol("x"),x[[3]]))
192 )
193 ))
194 oldcodes[!has.range] <- lapply(oldcodes[!has.range],
195 function(x)
196 as.call(list(as.symbol("%in%"),as.symbol("x"),x))
197 )
198 torecode <- sapply(oldcodes,eval,envir=environment())
199 if(!is.matrix(torecode)) torecode <- t(torecode)
200 newcodes <- sapply(newcodes,eval,parent.frame())
201 nevtrue <- colSums(torecode, na.rm=TRUE) == 0
202 if(any(nevtrue)){
203 nrecng <- paste(recodings[nevtrue])
204 if(length(nrecng)>1)
205 warning("recodings ",paste(nrecng,collapse=", ")," have no consequences")
206 else
207 warning("recoding ",nrecng," has no consequences")
208 }
209 ambiguous <- rowSums(torecode, na.rm=TRUE) > 1
210 if(any(ambiguous))
211 stop("recoding request is ambiguous")
212 y <- if(is.character(newcodes)) as.character(x) else x
213 newcodes <- newcodes[!nevtrue]
214 torecode <- torecode[,!nevtrue,drop=FALSE]
215 for(i in seq(along=newcodes)){
216 y[torecode[,i]] <- newcodes[i]
217 }
218 if(!copy){
219 if(is.character(otherwise)) newcodes <- c(newcodes, otherwise)
220 recoded <- as.logical(rowSums(torecode))
221 recoded[is.na(recoded)] <- TRUE
222 tmp <- as.vector(otherwise,mode=storage.mode(y))
223 length(otherwise) <- length(y)
224 otherwise[] <- tmp
225 y[!recoded] <- otherwise[!recoded]
226 nNA <- sum(is.na(otherwise[!recoded])) - sum(is.na(x[!recoded]))
227 if(nNA > 0)
228 warning("recoding created ",nNA," NAs")
229 if(!NA_recoded)
230 y[is.na(x)] <- NA
231 }
232 if(is.character(y)) {
233 levels <- sort(unique(y[y%nin%newcodes]))
234 levels <- c(newcodes,levels)
235 y <- factor(y,levels=levels)
236 }
237 return(y)
238 })
239
240 setMethod("recode","factor",function(x,...,copy=getOption("recode_copy",identical(otherwise,"copy")),
241 otherwise=NA){
242 recodings <- match.call(expand.dots=FALSE)$...
243
244 if(length(otherwise)!=1) stop("otherwise= argument must have length 1")
245
246 if("to.factor" %in% names(recodings)
247 && is.logical(recodings[["to.factor"]])
248 ) {
249 warning("'to.factor' argument no longer in use")
250 recodings["to.factor"] <- NULL
251 }
252 recodings <- recodings[nzchar(sapply(recodings,paste,collapse=""))]
253 if(any(sapply(sapply(recodings,"[[",1),paste)!="<-"))
254 stop("invalid recoding request")
255
256 if(!length(recodings)) return(x)
257 newcodes <- sapply(recodings,"[[",2)
258 if(copy)
259 newcodes <- as.character(newcodes) # 'copy=TRUE' requires the result also to be a factor ...
260 if(is.character(newcodes))
261 to_factor <- TRUE
262 else
263 to_factor <- FALSE
264 oldcodes <- lapply(recodings,"[[",3)
265 oldcodes <- lapply(oldcodes,eval,envir=environment())
266 torecode <- sapply(oldcodes,function(o) x %in% o)
267 if(!is.matrix(torecode)) torecode <- t(torecode)
268 nevtrue <- colSums(torecode) == 0
269 if(any(nevtrue)){
270 nrecng <- paste(recodings[nevtrue])
271 if(length(nrecng)>1)
272 warning("recodings ",paste(nrecng,collapse=", ")," have no consequences")
273 else
274 warning("recoding ",nrecng," has no consequences")
275 }
276 ambiguous <- rowSums(torecode) > 1
277 if(any(ambiguous))
278 stop("recoding request is ambiguous")
279
280 if(to_factor){
281 y <- integer(length=length(x))
282 for(i in 1:ncol(torecode)){
283 y[torecode[,i]] <- i
284 }
285 if(copy){
286 olevels <- levels(unique(x[y==0,drop=TRUE]))
287 max.y <- max(y)
288 for(i in seq(along=olevels)){
289 y[x == olevels[i]] <- max.y + i
290 }
291 y <- factor(y,levels=1:max(y),labels=c(newcodes,olevels))
292 }
293 else if(is.character(otherwise)){
294 max.y <- max(y)
295 y[y == 0] <- max.y + 1
296 y <- factor(y,levels=1:max(y),labels=c(newcodes,otherwise[1]))
297 }
298 else {
299 y <- factor(y,levels=1:max(y),labels=newcodes)
300 recoded <- as.logical(rowSums(torecode))
301 nNA <- sum(is.na(y[!recoded]) & !is.na(x[!recoded]))
302 if(nNA > 0)
303 warning("recoding created ",nNA," NAs")
304 }
305 }
306 else {
307 y <- vector(mode=storage.mode(newcodes),
308 length=length(x))
309 for(i in 1:ncol(torecode))
310 y[torecode[,i]] <- newcodes[i]
311 recoded <- as.logical(rowSums(torecode))
312 recoded[is.na(recoded)] <- TRUE
313 tmp <- as.vector(otherwise,mode=storage.mode(y))
314 length(otherwise) <- length(y)
315 otherwise[] <- tmp
316 y[!recoded] <- otherwise[!recoded]
317 nNA <- sum(is.na(y[!recoded]) & !is.na(x[!recoded]))
318 if(nNA > 0)
319 warning("recoding created ",nNA," NAs")
320 }
321 return(y)
322 })
323