File: getdata.r

package info (click to toggle)
r-cran-sampling 2.11-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,340 kB
  • sloc: ansic: 21; makefile: 2
file content (49 lines) | stat: -rw-r--r-- 1,682 bytes parent folder | download | duplicates (3)
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
47
48
49
getdata<-function(data, m) 
{
    if (!is.data.frame(data)) 
        data = as.data.frame(data)
    if (is.null(names(data))) 
        stop("the column names are missing")
    if (is.vector(m) & !is.list(m)) {
        res = NULL
        if (is.null(names(m))) 
            if (all(m %in% c(0, 1))) {
                res = NULL
                if (!("ID_unit" %in% names(data))) {
                  res = cbind.data.frame((1:length(m))[m == 1], 
                    data[m == 1, ])
                  names(res) = c("ID_unit", names(data))
                }
                else res = data[m == 1, ]
            }
            else  res=data[rep(which(m>0),m[m>0]),]                
    }
    else if (is.data.frame(m)) {
        res = NULL
        if (!is.null(names(m))) {
            mm = match(names(data), names(m), nomatch = 0)
            index = (1:ncol(data))[mm == 0]
            if (length(index) > 0) {
                res = cbind.data.frame(data[m$ID_unit, index], 
                  m)
                names(res)[1:length(index)] = names(data)[index]
            }
            else res = m
        }
    }
    else if (is.list(m)) {
        res = list()
        if (length(m) >= 1) 
            for (j in 1:length(m)) {
                mm = match(names(data), names(m[[j]]), nomatch = 0)
                index = (1:ncol(data))[mm == 0]
                if (length(index) > 0) {
                  res[[j]] = cbind.data.frame(data[m[[j]]$ID_unit, 
                    index], m[[j]])
                  names(res[[j]])[1:length(index)] = names(data)[index]
                }
            }
        else res = m
    }
    res
}