irtc_read: Read and Clean Item Response Data from Many Formats

View source: R/irtc_read.R

irtc_readR Documentation

Read and Clean Item Response Data from Many Formats

Description

Unified data import with automatic cleaning. Supports Excel (.xlsx, .xls; requires readxl), delimited text (.csv, .tsv, .txt, .dat; delimiter and UTF-8/GBK encoding are detected automatically), SPSS/Stata/SAS files (.sav, .por, .dta, .sas7bdat, .xpt; requires haven) and in-memory data frames, matrices and tibbles.

Every cleaning action is recorded in a bilingual log attached to the returned object, so results remain traceable.

Usage

irtc_read(x, sheet = 1, id = NULL, weights = NULL,
    missing_codes = c(-9, -99, 99, 999),
    na_strings = c("", "NA", "N/A", "n/a", ".", "*",
        "\u7f3a\u5931", "\u65e0", "\u7a7a"),
    guess_id = TRUE, guess_weights = TRUE, clean = TRUE, recode = TRUE,
    verbose = TRUE)

## S3 method for class 'irtc_data'
print(x, lang = irtc_lang(), ...)

Arguments

x

A file path or a data frame / matrix. For the print method: an irtc_data object.

sheet

Excel sheet to read (number or name).

id

Person identifier column(s), by name or index. The first is kept as the person ID; the rest are set aside. If NULL and guess_id is TRUE, common ID column names (id, pid, student number, name, etc., in English and Chinese) are detected automatically.

missing_codes

Numeric codes recoded to NA. Negative codes are always treated as missing; a positive code is only recoded in a column when it lies clearly above the observed response range, which protects e.g. a legitimate score of 99 on a 0-100 scale.

na_strings

Strings treated as missing in text columns.

weights

Sampling weight column, by name or index. If NULL and guess_weights is TRUE, common weight column names (weight, wt, pweight, \zh权重\u6743\u91cd, \zh样本权重\u6837\u672c\u6743\u91cd, etc., in English and Chinese) are detected automatically. The column is set aside as $weights and later used as pweights by irtc. Weights must be positive numbers; missing weights are set to 1 with a warning.

guess_id

Logical: detect the person ID column automatically?

guess_weights

Logical: detect the weight column automatically?

clean

Logical: apply the cleaning steps (drop empty rows/columns, convert numeric-like text, recode missing codes)?

recode

Logical: recode integer response categories to consecutive scores starting at 0 (e.g. a 1-5 Likert item becomes 0-4)?

verbose

Logical: print the object (including the cleaning log)?

lang

Output language, "zh" or "en".

...

Ignored.

Value

An object of class irtc_data: a list with components resp (cleaned response data frame), pid (person IDs or NULL), weights (sampling weights or NULL), dropped (set-aside non-response columns), log (cleaning log data frame with columns step, code, message_en, message_zh) and source.

See Also

irtc, irtc_score, irtc_check_data

Examples

path <- tempfile(fileext = ".csv")
writeLines(c("id,i1,i2,i3", "s1,0,1,99", "s2,1,-9,1", "s3,1,0,0"), path)
d <- irtc_read(path, verbose = FALSE)
d$resp
d$pid
print(d, lang = "en")
unlink(path)

IRTC documentation built on July 24, 2026, 5:07 p.m.