Survey exports often encode metadata in the filename (e.g.
"podracing_wave1_NA_2026.csv"). This splits a filename column on a
separator into named metadata columns, dropping the file extension first.
Replaces the brittle str_split(file, "_")[[1]][n] indexing this saves you.
Arguments
- data
A data frame.
- col
Name of the column holding the filename (string or unquoted). Defaults to
"file".- into
Character vector of new column names, one per field you expect after splitting.
- sep
Separator to split on (a fixed string, not a regex). Defaults to
"_".- drop_ext
If
TRUE(default), strip a trailing file extension before splitting.- remove
If
TRUE, drop the original filename column. Defaults toFALSEso you keep the source reference.
Details
Pairs naturally with read_folder(), whose file column carries the source
filename: split it once and you have event, wave, locale, year, etc. as real
columns to group by. The file extension is stripped first (drop_ext), the
split is on a fixed string (not a regex), and rows with fewer fields than
into are padded with NA so a stray filename never derails the parse.
See also
Other import:
read_folder(),
select_prefix(),
select_suffix()
