| 1 |
spss.readheader <- function(f) |
| 2 |
.Call("read_sysfile_header",f) |
| 3 |
|
| 4 |
spss.readvars <- function(f,n){ |
| 5 |
ans <- list() |
| 6 |
|
| 7 |
if(n > 0){ |
| 8 |
for(i in 1:n){ |
| 9 |
currvar <- .Call("read_sysfile_var",f) |
| 10 |
ans <- c(ans,list(currvar)) |
| 11 |
} |
| 12 |
} |
| 13 |
else { |
| 14 |
repeat { |
| 15 |
currvar <- .Call("read_sysfile_var",f) |
| 16 |
if(!is.null(currvar)) |
| 17 |
ans <- c(ans,list(currvar)) |
| 18 |
else break |
| 19 |
} |
| 20 |
} |
| 21 |
names(ans) <- sapply(ans,function(x)x$name) |
| 22 |
ans |
| 23 |
} |
| 24 |
|
| 25 |
spss.testcode <- function(f) |
| 26 |
.Call("test_sysfile_int32",f) |
| 27 |
|
| 28 |
spss.readlabels <- function(f){ |
| 29 |
ans <- list() |
| 30 |
while(spss.testcode(f)==3){ |
| 31 |
ans <- c(ans,list(.Call("read_sysfile_value_labels",f))) |
| 32 |
} |
| 33 |
ans |
| 34 |
} |
| 35 |
|
| 36 |
spss.readdocument <- function(f) |
| 37 |
.Call("read_sysfile_document",f) |
| 38 |
|
| 39 |
spss.readaux <- function(f) |
| 40 |
.Call("read_sysfile_aux",f) |
| 41 |
|
| 42 |
spss.dictterm <- function(f) |
| 43 |
.Call("read_sysfile_dict_term",f) |
| 44 |
|
| 45 |
parseSysHeader <- function(file){ |
| 46 |
header <- spss.readheader(file) |
| 47 |
swapcode <- header$swap_code |
| 48 |
nvars <- header$case_size |
| 49 |
attr(file,"swap_code") <- swapcode |
| 50 |
attr(file,"case_size") <- nvars |
| 51 |
attr(file,"bias") <- header$bias |
| 52 |
attr(file,"compressed") <- header$compressed |
| 53 |
variables <- spss.readvars(file,nvars) |
| 54 |
if(nvars == 0){ |
| 55 |
warning("File header does not contain information on the number of variables. |
| 56 |
This is a bug in the software (ReadStat/haven?) with which the data file was created. |
| 57 |
Guessing that number.", |
| 58 |
call.=FALSE,immediate.=TRUE) |
| 59 |
nvars <- length(variables) |
| 60 |
attr(file,"case_size") <- nvars |
| 61 |
} |
| 62 |
|
| 63 |
value.labels <- NULL |
| 64 |
document <- NULL |
| 65 |
if(spss.testcode(file)==3) |
| 66 |
value.labels <- spss.readlabels(file) |
| 67 |
if(spss.testcode(file)==6) |
| 68 |
document <- spss.readdocument(file) |
| 69 |
|
| 70 |
auxiliaries <- list() |
| 71 |
while(spss.testcode(file)==7){ |
| 72 |
aux <- spss.readaux(file) |
| 73 |
auxtype <- aux$type |
| 74 |
aux <- list(aux$data) |
| 75 |
names(aux) <- auxtype |
| 76 |
auxiliaries <- c(auxiliaries,aux) |
| 77 |
} |
| 78 |
if(length(auxiliaries$info_flt64)){ |
| 79 |
|
| 80 |
sysmis <- auxiliaries$info_flt64["sysmis"] |
| 81 |
highest <- auxiliaries$info_flt64["highest"] |
| 82 |
lowest <- auxiliaries$info_flt64["lowest"] |
| 83 |
} |
| 84 |
else{ |
| 85 |
|
| 86 |
warning("file lacks info_flt64 record, using defaults") |
| 87 |
|
| 88 |
info_flt64 <- .Call("dflt_info_flt64",file) |
| 89 |
sysmis <- info_flt64["sysmis"] |
| 90 |
highest <- info_flt64["highest"] |
| 91 |
lowest <- info_flt64["lowest"] |
| 92 |
} |
| 93 |
attr(file,"sysmis") <- sysmis |
| 94 |
attr(file,"highest") <- highest |
| 95 |
attr(file,"lowest") <- lowest |
| 96 |
if(spss.testcode(file)==999) start.data <- spss.dictterm(file) |
| 97 |
else stop("did not find dictionary termination code") |
| 98 |
#message("\nstart of data:",p$start.data) |
| 99 |
attr(file,"sysmis") <- sysmis |
| 100 |
attr(file,"data_pos") <- start.data |
| 101 |
|
| 102 |
if(length(auxiliaries$longVariableNames)){ |
| 103 |
longVariableNames <- auxiliaries$longVariableNames |
| 104 |
longVariableNames <- strsplit(longVariableNames,"\t")[[1]] |
| 105 |
longVariableNames <- strsplit(longVariableNames,"=") |
| 106 |
longVariableNames <- sapply(longVariableNames,function(lvn) |
| 107 |
structure(lvn[2],names=lvn[1])) |
| 108 |
ii <- match(names(longVariableNames),names(variables)) |
| 109 |
names(variables)[ii] <- unname(longVariableNames) |
| 110 |
} |
| 111 |
|
| 112 |
variables <- lapply(variables,function(x)x[-1]) |
| 113 |
|
| 114 |
missings <- list() |
| 115 |
|
| 116 |
missings$values <- lapply(variables,"[[","missings") |
| 117 |
missings$ranges <- vector(length(variables),mode="list") |
| 118 |
names(missings$ranges) <- names(missings$values) |
| 119 |
n_missings <- sapply(variables,"[[","n_missing_values") |
| 120 |
|
| 121 |
mrang <- missings$values[n_missings == -2] |
| 122 |
if(length(mrang)){ |
| 123 |
mrang <- do.call(rbind,mrang) |
| 124 |
is.lo <- mrang[,1] == lowest |
| 125 |
is.hi <- mrang[,2] == highest |
| 126 |
if(length(is.lo)) |
| 127 |
mrang[is.lo,1] <- -Inf |
| 128 |
if(length(is.hi)) |
| 129 |
mrang[is.hi,2] <- Inf |
| 130 |
missings$ranges[n_missings == -2] <- split(mrang,row(mrang)) |
| 131 |
} |
| 132 |
mrang_val <- missings$values[n_missings == -3] |
| 133 |
if(length(mrang_val)){ |
| 134 |
mrang_val <- do.call(rbind,mrang_val) |
| 135 |
|
| 136 |
is.lo <- mrang_val[,1] == lowest |
| 137 |
is.hi <- mrang_val[,2] == highest |
| 138 |
if(length(is.lo)) |
| 139 |
mrang_val[is.lo,1] <- -Inf |
| 140 |
if(length(is.hi)) |
| 141 |
mrang_val[is.hi,2] <- Inf |
| 142 |
missings$values[n_missings == -3] <- split(unname(mrang_val[,3,drop=FALSE]),seq_len(nrow(mrang_val))) |
| 143 |
missings$ranges[n_missings == -3] <- split(mrang_val[,1:2,drop=FALSE],row(mrang_val[,1:2,drop=FALSE])) |
| 144 |
} |
| 145 |
missings$values[n_missings == -2] <- list(NULL) |
| 146 |
|
| 147 |
types <- sapply(variables,function(x)x$type) |
| 148 |
digits <- sapply(variables,function(x)x$print[4]) |
| 149 |
list( |
| 150 |
header = header, |
| 151 |
auxiliaries = auxiliaries, |
| 152 |
variables = variables[types>=0], |
| 153 |
value.labels = value.labels, |
| 154 |
missings = missings, |
| 155 |
types = types, |
| 156 |
document = document |
| 157 |
) |
| 158 |
} |
| 159 |
|
| 160 |
# seekSysData <- function(p) |
| 161 |
# .Call("rewind_sysfile",p$file) |
| 162 |
|
| 163 |
# spss_numeric_to_POSIXct <- function(x){ |
| 164 |
# x <- as.numeric(x) |
| 165 |
# as.POSIXct(x, origin = "1582-10-14", tz = "GMT") |
| 166 |
# } |
| 167 |
|
| 168 |
spss.string.vallabs <- function(vallab) |
| 169 |
structure( |
| 170 |
trimws(.Call("num_to_string8",vallab)), |
| 171 |
names=names(vallab) |
| 172 |
) |