split_fill | R Documentation |
Split a chr vector based on sep, return melted DT by ID
split_fill(dat, targ, split_on, IDcol, rebind = FALSE, keep.targ = FALSE, ...)
dat |
a data.table |
targ |
chr; vector of length 1 denoting column that contains data to be split |
split_on |
chr; what pattern should be used to perform the split? |
IDcol |
chr; vector of length 1 denoting the column in dat containing the ID to be used for melting |
rebind |
logical; should the original columns be appended back to the output? Defaults to |
keep.targ |
logical; only used if rebind = |
... |
Other (prefereably named) arguments to pass on to |
This is a convenience-convenience (not a typo) wrapper around data.table::tstrsplit
, taking advantage
of the performance of data.table::Ctranspose
, and adding faculties to melt and rejoin selectively.
A melted data.table using IDcol as id.var
for melt.data.table
,
with targ splitted by split_on.
If rebind == TRUE
, will also return the original columns, with a single IDcol as denoted
in input. This is performed via a data.table
ad-hoc join, using IDcol in j
. The input
targ column will be returned as well, if keep.targ is TRUE
.
targ currently is limited to a vector of length 1, as is IDcol. This is likely to change in the future, to
make this function more flexible and consistent with the capabilities of melt.data.table
.
Use ...
to pass e.g. fixed = TRUE
or perl = TRUE
to strsplit
. See documentation
for tstrsplit
.
library(data.table) dt <- data.table( ID = 1:10, targ = sapply(1:10, function(f) paste0( LETTERS[1:5], f, collapse = "|" ) ) ) head(split_fill(dat = dt, targ = "targ", split_on = "\\|", IDcol = "ID")) #Demonstrating rebind dt[, targ_additional := targ] head(split_fill(dat = dt, targ = "targ", split_on = "\\|", IDcol = "ID", rebind = TRUE))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.