Debian Codegraph @

man/importers.Rd

raw
1 \name{importers}
2 \alias{importer}
3 \alias{importer-class}
4 \alias{spss.file}
5 \alias{spss.portable.file}
6 \alias{spss.portable.importer-class}
7 \alias{show,spss.portable.importer-method}
8 \alias{spss.system.file}
9 \alias{spss.system.importer-class}
10 \alias{show,spss.system.importer-method}
11 \alias{spss.fixed.file}
12 \alias{spss.fixed.importer-class}
13 \alias{show,spss.fixed.importer-method}
14 \alias{Stata.file}
15 \alias{Stata.importer-class}
16 \alias{show,Stata.importer-method}
17 \alias{Stata_new.importer-class}
18 \alias{show,Stata_new.importer-method}
19 \alias{as.data.set,importer-method}
20 \alias{dim,importer-method}
21 \alias{names,importer-method}
22 \alias{[,importer,atomic,atomic,ANY-method}
23 \alias{[,importer,atomic,missing,ANY-method}
24 \alias{[,importer,missing,atomic,ANY-method}
25 \alias{[,importer,missing,missing,ANY-method}
26 \alias{[[,importer-method}
27 \alias{$,importer-method}
28 \alias{head,importer-method}
29 \alias{tail,importer-method}
30 \alias{initialize,spss.portable.importer-method}
31 \alias{initialize,spss.system.importer-method}
32 \alias{initialize,spss.fixed.importer-method}
33 \alias{initialize,Stata.importer-method}
34 \alias{initialize,Stata_new.importer-method}
35 \alias{subset.spss.portable.importer}
36 \alias{subset.spss.system.importer}
37 \alias{subset.spss.fixed.importer}
38 \alias{subset.Stata.importer}
39 \alias{subset.Stata_new.importer}
40
41 \title{Object Oriented Interface to Foreign Files}
42 \description{
43 Importer objects are objects that refer to an external
44 data file. Currently only Stata files,
45 SPSS system, portable, and fixed-column files are supported.
46
47 Data are actually imported by `translating' an
48 importer file into a \code{\link{data.set}} using
49 \code{as.data.set} or \code{subset}.
50
51 The \code{importer} mechanism is more flexible and extensible
52 than \code{\link[foreign]{read.spss}} and \code{\link[foreign]{read.dta}}
53 of package "foreign", as most of the parsing of the file headers is done in R.
54 It is also adapted to efficiently load large data sets.
55 Most importantly, importer objects support the
56 \code{\link{labels}}, \code{\link{missing.values}},
57 and \code{\link{description}}s, provided by this package.
58 }
59 \usage{
60 spss.file(file,\dots)
61
62 spss.fixed.file(file,
63 columns.file,
64 varlab.file=NULL,
65 codes.file=NULL,
66 missval.file=NULL,
67 count.cases=TRUE,
68 to.lower=getOption("spss.fixed.to.lower",FALSE),
69 iconv=TRUE,
70 encoded=getOption("spss.fixed.encoding","cp1252"),
71 negative2missing = FALSE)
72
73 spss.portable.file(file,
74 varlab.file=NULL,
75 codes.file=NULL,
76 missval.file=NULL,
77 count.cases=TRUE,
78 to.lower=getOption("spss.por.to.lower",FALSE),
79 iconv=TRUE,
80 encoded=getOption("spss.por.encoding","cp1252"),
81 negative2missing = FALSE)
82
83 spss.system.file(file,
84 varlab.file=NULL,
85 codes.file=NULL,
86 missval.file=NULL,
87 count.cases=TRUE,
88 to.lower=getOption("spss.sav.to.lower",FALSE),
89 iconv=TRUE,
90 encoded=getOption("spss.sav.encoding","cp1252"),
91 ignore.scale.info = FALSE,
92 negative2missing = FALSE)
93
94 Stata.file(file,
95 iconv=TRUE,
96 encoded=if(new_format)
97 getOption("Stata.new.encoding","utf-8")
98 else getOption("Stata.old.encoding","cp1252"),
99 negative2missing = FALSE)
100
101 ## The most important methods for "importer" objects are:
102 \method{subset}{spss.system.importer}(x, subset, select, drop = FALSE, \dots)
103 \method{subset}{spss.portable.importer}(x, subset, select, drop = FALSE, \dots)
104 \method{subset}{spss.fixed.importer}(x, subset, select, drop = FALSE, \dots)
105 \method{subset}{Stata.importer}(x, subset, select, drop = FALSE, \dots)
106 \method{subset}{Stata_new.importer}(x, subset, select, drop = FALSE, \dots)
107
108 \S4method{as.data.set}{importer}(x,row.names=NULL,optional=NULL,
109 compress.storage.modes=FALSE,\dots)
110
111 \S4method{head}{importer}(x,n=20,\dots)
112 \S4method{tail}{importer}(x,n=20,\dots)
113 }
114 \arguments{
115 \item{file}{character string; the path to the file containing
116 the data}
117 \item{\dots}{Other arguments. \code{spss.file()} passes them on to
118 \code{spss.portable.file()} of \code{spss.system.file()}. Other
119 function ignore further arguments.}
120 \item{columns.file}{character string; the path to an
121 SPSS/PSPP syntax file with a \code{DATA LIST FIXED} statement}
122 \item{varlab.file}{character string; the path to an
123 SPSS/PSPP syntax file with a \code{VARIABLE LABELS} statement}
124 \item{codes.file}{character string; the path to an
125 SPSS/PSPP syntax file with a \code{VALUE LABELS} statement}
126 \item{missval.file}{character string; the path to an
127 SPSS/PSPP syntax file with a \code{MISSING VALUES} statement}
128 \item{count.cases}{logical; should cases in file be counted? This
129 takes effect only if the data file does not already contain information
130 about the number of cases.}
131 \item{to.lower}{logical; should variable names changed to lower
132 case?}
133 \item{iconv}{logical; should strings (in labels and
134 variables) changed into encoding of the platform?}
135 \item{encoded}{a cacharacter string; the way characters are encoded
136 in the improrted file. For the available encoding options
137 see \code{?iconvlist}. Using this argument for
138 \code{spss.system.file} this is only a fallback, as the function
139 uses the encoding information present in the file if it is
140 present.}
141 \item{negative2missing}{logical; should negative values be marked
142 as missing values? This is the convention of some newer data sets that
143 are available e.g. from the GESIS data archive.}
144 \item{ignore.scale.info}{logical; should information about measuremnt
145 scale levels provided in the file be ignored?}
146 \item{x}{an object that inherits from class \code{"importer"}.}
147 \item{subset}{a logical vector or an expression containing variables
148 from the external data file that evaluates to logical. }
149 \item{select}{a vector of variable names from the external data file.
150 This may also be a named vector, where the names give
151 the names into which the variables from the external data
152 file are renamed.}
153 \item{drop}{a logical value, that determines what happens if
154 only one column is selected. If TRUE and only one column
155 is selected, \code{subset} returns only a single \code{item}
156 object and not a \code{data.set}.}
157 \item{row.names}{ignored, present only for compatibility.}
158 \item{optional}{ignored, present only for compatibility.}
159 \item{compress.storage.modes}{logical value; if TRUE floating point values
160 are converted to integers if possible without loss of information.}
161 \item{n}{integer; the number of rows to be shown by \code{head} or \code{tail}}
162 }
163 \value{
164 \code{spss.fixed.file}, \code{spss.portable.file},
165 \code{spss.system.file}, and \code{Stata.file}
166 return, respectively, objects of class
167 \code{"spss.fixed.importer"}, \code{"spss.portable.importer"},
168 \code{"spss.system.importer"}, \code{"Stata.importer"}, or \code{"Stata_new.importer"},
169 which, by inheritance, are also objects of class \code{"importer"}.
170 \code{"Stata.importer"} is for files in the format of Stata versions up
171 to 12, while \code{"Stata_new.importer"} is for files in the newer
172 format of Stata versions from 13.
173
174 Objects of class \code{"importer"} have at least the following two slots:
175
176 \item{ptr}{an external pointer}
177 \item{variables}{a list of objects of class \code{"item.vector"} which
178 provides a `prototype' for the \code{"data.set"} set objects returned
179 by the \code{as.data.set} and \code{subset} methods for objects of
180 class \code{"importer"} }
181
182
183 The \code{as.data.frame} for \code{importer} objects does
184 the actual data import and returns a data frame. Note that in contrast
185 to \code{\link[foreign]{read.spss}}, the variable names of the
186 resulting data frame will be lower case, unless the importer function
187 is called with \code{to.lower=FALSE}. If long variable names
188 are defined (in case of a PSPP/SPSS system file), they take
189 precedence and are \emph{not} coerced to lower case.
190 }
191 \seealso{ \code{\link{codebook}}, \code{\link{description}},
192 \code{\link[foreign]{read.spss}}
193 }
194 \details{
195 A call to a `constructor' for an importer object, that is,
196 \code{spss.fixed.file}, \code{spss.portable.file}, \code{spss.sysntax.file},
197 or \code{Stata.file},
198 causes R to read in the header of the data file and/or
199 the syntax files that contain information about
200 the variables, such as the columns that they occupy
201 (in case of \code{spss.fixed.file}), variable labels,
202 value labels and missing values.
203
204 The information in the file header and/or the accompagnying
205 files is then processed to prepare the file for importing.
206 Thus the inner structure of an \code{importer} object may
207 well vary according to what type of file is to imported and
208 what additional information is given.
209
210 The \code{as.data.set} and \code{subset} methods
211 for \code{"importer"} objects internally use the
212 generic functions \code{seekData}, \code{readData}, \code{readSlice},
213 and \code{readChunk}, which have methods for the
214 subclasses of \code{"importer"}.
215 These functions are not callable
216 from outside the package, however.
217
218 The \code{subset} method for \code{"importer"} objects reads in
219 the data `chunk-wise' to create the subset of observations if
220 the option \code{"subset.chunk.size"} is set to a non-\code{NULL}
221 value, e.g. by \code{options(subset.chunk.size=1000)}. This may be
222 useful in case of very large data sets from which only a tiny subset
223 of observations is needed for analysis.
224
225 Since the functions described here are more or less complete rewrite
226 based on the description of the file structure provided
227 by the documenation for PSPP, they are perhaps not as thorougly tested as the
228 functions in the \code{foreign} package, apart from the frequent use
229 by the author of this package.
230 }
231 \examples{
232 # Extract American National Election Study of 1948
233 nes1948.por <- unzip(system.file("anes/NES1948.ZIP",package="memisc"),
234 "NES1948.POR",exdir=tempfile())
235
236 # Get information about the variables contained.
237 nes1948 <- spss.portable.file(nes1948.por)
238
239 # The data are not yet loaded:
240 show(nes1948)
241
242 # ... but one can see what variables are present:
243 description(nes1948)
244
245 # Now a subset of the data is loaded:
246 vote.socdem.48 <- subset(nes1948,
247 select=c(
248 V480018,
249 V480029,
250 V480030,
251 V480045,
252 V480046,
253 V480047,
254 V480048,
255 V480049,
256 V480050
257 ))
258
259 # Let's make the names more descriptive:
260 vote.socdem.48 <- rename(vote.socdem.48,
261 V480018 = "vote",
262 V480029 = "occupation.hh",
263 V480030 = "unionized.hh",
264 V480045 = "gender",
265 V480046 = "race",
266 V480047 = "age",
267 V480048 = "education",
268 V480049 = "total.income",
269 V480050 = "religious.pref"
270 )
271
272 # It is also possible to do both
273 # in one step:
274 # vote.socdem.48 <- subset(nes1948,
275 # select=c(
276 # vote = V480018,
277 # occupation.hh = V480029,
278 # unionized.hh = V480030,
279 # gender = V480045,
280 # race = V480046,
281 # age = V480047,
282 # education = V480048,
283 # total.income = V480049,
284 # religious.pref = V480050
285 # ))
286
287
288
289 # We examine the data more closely:
290 codebook(vote.socdem.48)
291
292 # ... and conduct some analyses.
293 #
294 t(genTable(percent(vote)~occupation.hh,data=vote.socdem.48))
295
296 # We consider only the two main candidates.
297 vote.socdem.48 <- within(vote.socdem.48,{
298 truman.dewey <- vote
299 valid.values(truman.dewey) <- 1:2
300 truman.dewey <- relabel(truman.dewey,
301 "VOTED - FOR TRUMAN" = "Truman",
302 "VOTED - FOR DEWEY" = "Dewey")
303 })
304
305 summary(truman.relig.glm <- glm((truman.dewey=="Truman")~religious.pref,
306 data=vote.socdem.48,
307 family="binomial",
308 ))
309 }
310 \keyword{file}