View source: R/misc_functions.R
replace_from_table | R Documentation |
'replace_from_table' replace multiple values in a data.table column.
. The values in from
argument are matched
and replaced by those in to
argument.
If newcolname = NULL
the replace is by reference.
replace_from_table(dtb, colname, from, to, newcolname = NULL)
dtb |
A data.table to be changed by reference. |
colname |
A string denoting the name of the column to be changed. |
from |
A vector with values in |
to |
A vector with values in |
newcolname |
A string denoting the name of a new column
to be created. If present, |
a data.table, invisibly.
library(data.table)
library(CKutils)
dtb <- data.table::data.table("a" = 1:5, "b" = seq(1, 2.2, 0.3),
"d" = letters[1:5])
dtb[, e := factor(a, labels = LETTERS[1:5])]
replace_from_table(data.table::copy(dtb), "a", 1:3, 3L)[]
replace_from_table(data.table::copy(dtb), "a", 3L, -11L)[]
replace_from_table(data.table::copy(dtb), "a", 3L, -11L, "newcol")[]
replace_from_table(data.table::copy(dtb), "b", 1.3, "a")[]
replace_from_table(data.table::copy(dtb), "b", 1.3, "a", "newcol")[]
replace_from_table(data.table::copy(dtb), "d", "a", "7")[]
replace_from_table(data.table::copy(dtb), "d", "a", 7)[]
replace_from_table(data.table::copy(dtb), "e", "B", "J")[]
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.