File: napass0.R

package info (click to toggle)
r-cran-lava 1.8.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,816 kB
  • sloc: sh: 13; makefile: 2
file content (46 lines) | stat: -rw-r--r-- 1,354 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46

impute0 <- function(object,rows,idx,na.action=na.omit,value,...) {
    if (missing(rows) && missing(idx)) {
        df <- na.action(object,...) 
        rows <- attr(df,"na.action")  
    }
    if (!missing(idx)) {
        obs1 <- setdiff(seq(length(object)),idx)[1]
    } else {
        obs1 <- setdiff(seq(NROW(object)),rows)[1]
    }
    if (missing(value)) {
        fobs <- object[obs1]
        if (is.logical(fobs)) value <- FALSE
        else if (is.character(fobs)) value <- fobs
        else if (is.factor(fobs)) value <- levels(fobs)[1]
        else value <- 0
    }
    if (!missing(idx)) {
        object[idx] <- value
        return(object)
    }
    if (is.matrix(object)) {
        object[rows,] <- value
    } else {
        object[rows] <- value
    }
    return(object)
}

##' @export 
na.pass0 <- function(object,all=TRUE,na.action=na.omit, ...) {
    ## Fill in "zeros" in the design matrix where we have missing data    
    df <- na.action(object,...)
    idx <- attr(df,"na.action")
    if (is.matrix(object) || is.vector(object)) {
        object <- impute0(object,rows=idx,...)
    } else {
        for (i in seq_len(NCOL(object))) {
            object[[i]] <- impute0(object[[i]],rows=idx,...)
        }
    }
    if (!is.null(idx))
        return(structure(object,na.action=structure(idx,class="pass0")))
    return(object)
}