Reduces IRW long-format data to one row per id, keeping the columns
that describe the person rather than the response. Optionally aligns those
rows to the row order of a wide response matrix, which is what most
psychometric packages need when a grouping variable is passed alongside a
response matrix.
Arguments
- df
A data frame in IRW long format, containing an
idcolumn.- cols
Character vector of person-level columns to extract. Defaults to
NULL, meaning detect them automatically as described above.- align
Optional. A data frame with an
idcolumn (such as the result ofirw_long2resp()), a matrix withidrownames, or a vector of ids. The returned rows are put in this order, one row per element, so the result lines up with the rows of a wide response matrix.
Value
A data frame with one row per id and columns id
followed by the person-level covariates. When align is supplied,
rows follow that order and ids not present in df yield NA
rows.
Details
irw_long2resp() keeps only id, item, and the response
column, so person-level covariates are dropped and the row order of its
result is not sorted. Aligning a covariate to that result by hand requires a
match() on id, and getting it wrong silently misassigns people
to groups. Passing the wide result as align does that match for you.
When cols is NULL, the person-level columns are detected
automatically: every column other than id and the response-level
columns (item, resp, rater, rt, text,
date, source_table) that takes exactly one value within every
id. A column with a value for some of a person's rows and NA
for others counts as varying and is not selected. Note that wave is
treated as person-level only when each person appears in a single wave.
Examples
df <- data.frame(
id = c(1, 1, 2, 2),
item = c("i1", "i2", "i1", "i2"),
resp = c(1, 0, 1, 1),
cov_group = c("A", "A", "B", "B"),
stringsAsFactors = FALSE
)
irw_covariates(df)
#> id cov_group
#> 1 1 A
#> 2 2 B
# Aligned to the row order of a wide response matrix
wide <- irw_long2resp(df, id_density_threshold = NULL)
irw_covariates(df, align = wide)
#> id cov_group
#> 1 1 A
#> 2 2 B