Reverses srv_pivot_longer() with keep_attributes = TRUE,
rebuilding one column per name and restoring each column's original type and
metadata from the carried attributes list-column. Factors are rebuilt with
their original levels/ordered; numeric survey types (fn, weight) are
coerced back to numeric; every other attribute (notably label and the survey
metadata) is re-attached. This is what makes the reshaped columns usable
downstream — e.g. a Crunch upload needs real factor levels for value labels and
attr(x, "label") for the variable description.
Arguments
- data
A long survey frame produced by
srv_pivot_longer(..., keep_attributes = TRUE). Must containname,responseandattributes, plus every column named inid_cols.- id_cols
Character vector of identifier column(s) that key each wide row. Defaults to
"response_id". Only these columns are carried through; anything else indata(e.g.label, helper columns) is dropped —left_join()the result back onto your main frame if you need the rest.
Examples
if (FALSE) { # \dontrun{
data("srv_example", package = "g6tr.survey")
x <- srv_example |> step_set_sr(s_gender)
# Lengthen (carrying attributes), edit labels / metadata in long form if
# needed, then widen back — types and metadata are restored.
long <- x |>
dplyr::select(response_id, s_gender) |>
srv_pivot_longer(-response_id, keep_attributes = TRUE)
wide <- srv_pivot_wider(long)
levels(wide$s_gender) # factor levels restored
get_var_metadata(wide$s_gender)$type # "sr" metadata restored
} # }