| Title: | Read and Export 'Nesstar' Binary Files |
|---|---|
| Description: | Reads '.Nesstar' binary files into data frames. '.Nesstar' is the file format of 'Nesstar Publisher', survey documentation software from the International Household Survey Network <https://www.ihsn.org/node/557>. Statistical agencies, for example the Ministry of Statistics and Programme Implementation ('MoSPI', India), publish survey microdata in this format. |
| Authors: | Saket Choudhary [aut, cre, cph] (ORCID: <https://orcid.org/0000-0001-5202-7633>) |
| Maintainer: | Saket Choudhary <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 0.1.0 |
| Built: | 2026-10-17 19:36:13 UTC |
| Source: | https://github.com/saketkc/nesstarR |
Compares one dataset of a Nesstar file with the same data from a different source, for example a Stata or SPSS export. Use this function to make sure that nesstarR decodes a file correctly.
nesstar_compare(x, dataset_number, reference)nesstar_compare(x, dataset_number, reference)
x |
A |
dataset_number |
Integer dataset number. |
reference |
A data frame with the same rows, in the same order. |
The function matches the columns by name. Before the match, it changes
each group of characters that are not letters or digits to _, removes
_ from the start and the end, and changes the names to upper case.
Thus, Srl._No. and Srl_No are the same column.
If all values in the two columns are numbers, the function compares the
numbers. Thus, "010" and 10 are equal. Otherwise, it compares
the values as text, without leading and trailing spaces. Empty strings and
NA are equal.
Remove value labels from reference before the comparison, for
example with haven::zap_labels().
A data frame with one row for each column name and these columns:
columnThe column name after the change.
in_nesstar, in_reference
TRUE if the
column is in the source.
different_valuesThe number of rows with different
values. NA if the column is in only one source.
path <- system.file("extdata", "example.Nesstar", package = "nesstarR") x <- nesstar_parse(path) reference <- nesstar_read_dataset(x, dataset_number = 1) reference$AGE[1] <- 26 nesstar_compare(x, dataset_number = 1, reference = reference)path <- system.file("extdata", "example.Nesstar", package = "nesstarR") x <- nesstar_parse(path) reference <- nesstar_read_dataset(x, dataset_number = 1) reference$AGE[1] <- 26 nesstar_compare(x, dataset_number = 1, reference = reference)
List the datasets in a Nesstar file
nesstar_datasets(x)nesstar_datasets(x)
x |
A |
A data frame with columns dataset_number, row_count,
variable_count.
path <- system.file("extdata", "example.Nesstar", package = "nesstarR") x <- nesstar_parse(path) nesstar_datasets(x)path <- system.file("extdata", "example.Nesstar", package = "nesstarR") x <- nesstar_parse(path) nesstar_datasets(x)
Writes each dataset to a different CSV file. The file name contains the name of the Nesstar file and the dataset number.
nesstar_export( x, output_dir, datasets = NULL, compress = TRUE, chunk_size = 50000L )nesstar_export( x, output_dir, datasets = NULL, compress = TRUE, chunk_size = 50000L )
x |
A |
output_dir |
Directory for the output files. If the directory does not exist, the function makes it. |
datasets |
Integer vector of the dataset numbers to export. If
|
compress |
Logical. If |
chunk_size |
Number of rows to read at a time. A smaller value decreases the memory use. |
Invisibly, a character vector of output file paths.
path <- system.file("extdata", "example.Nesstar", package = "nesstarR") x <- nesstar_parse(path) out <- nesstar_export(x, output_dir = tempdir()) read.csv(out[1])path <- system.file("extdata", "example.Nesstar", package = "nesstarR") x <- nesstar_parse(path) out <- nesstar_export(x, output_dir = tempdir()) read.csv(out[1])
Decodes the Huffman-coded XML blocks in the file. The XML blocks contain the dataset file names, the variable labels and the category labels.
nesstar_metadata(x)nesstar_metadata(x)
x |
A |
Some older NSS files use different record IDs. For these files, the
function cannot decode the metadata. It gives a warning and returns
NULL.
A named list:
datasetsA list with one element for each dataset. Each
element contains dataset_number, file_name and
variables. variables is a list with one list for each
variable. Each variable list contains name, label and
categories.
If the function cannot decode the metadata, it returns NULL.
path <- system.file("extdata", "example.Nesstar", package = "nesstarR") x <- nesstar_parse(path) meta <- nesstar_metadata(x) meta$datasets[[1]]$variables[[1]]$labelpath <- system.file("extdata", "example.Nesstar", package = "nesstarR") x <- nesstar_parse(path) meta <- nesstar_metadata(x) meta$datasets[[1]]$variables[[1]]$label
Decodes the resource index, the dataset descriptors and the variable directories. Does not decode row data.
nesstar_parse(path)nesstar_parse(path)
path |
Path to a |
The function does not keep the file in memory. The other functions read only the bytes that they need from the file. Thus, the memory use does not increase with the file size. Do not move or change the file while you use the result.
A nesstar_binary list (S3 class).
path <- system.file("extdata", "example.Nesstar", package = "nesstarR") x <- nesstar_parse(path) xpath <- system.file("extdata", "example.Nesstar", package = "nesstarR") x <- nesstar_parse(path) x
Numeric columns become numeric and string columns become
character. Older files can mark numeric columns as strings (mode 0 or
1); these are detected from their data size. Missing values become NA.
nesstar_read_dataset(x, dataset_number, columns = NULL)nesstar_read_dataset(x, dataset_number, columns = NULL)
x |
A |
dataset_number |
Integer dataset number. |
columns |
Character vector of the column names to read. If
|
A data.frame.
path <- system.file("extdata", "example.Nesstar", package = "nesstarR") x <- nesstar_parse(path) nesstar_read_dataset(x, dataset_number = 1) nesstar_read_dataset(x, dataset_number = 1, columns = c("AGE", "NAME"))path <- system.file("extdata", "example.Nesstar", package = "nesstarR") x <- nesstar_parse(path) nesstar_read_dataset(x, dataset_number = 1) nesstar_read_dataset(x, dataset_number = 1, columns = c("AGE", "NAME"))
List variables in one dataset
nesstar_variables(x, dataset_number)nesstar_variables(x, dataset_number)
x |
A |
dataset_number |
Integer dataset number (as shown by
|
A data frame with one row for each variable. The columns give the storage format of the variable.
path <- system.file("extdata", "example.Nesstar", package = "nesstarR") x <- nesstar_parse(path) nesstar_variables(x, dataset_number = 1)path <- system.file("extdata", "example.Nesstar", package = "nesstarR") x <- nesstar_parse(path) nesstar_variables(x, dataset_number = 1)