do_cols_dt | R Documentation |
This function performs arithmetic or other operations on a subset of columns in a data.table. It wraps the specified column names in backticks, concatenates them using the provided operator, and optionally applies a function to the resulting expression.
do_cols_dt(dtb, cols_to_add, symbol = c(",", "+", "-", "*", "/"), fn = NULL)
dtb |
A data.table. |
cols_to_add |
A character vector of column names to be operated on. |
symbol |
A character string specifying the operator to use between the columns. Defaults to '+' (if a vector is provided, the first element is used). Other examples include '-', '*', '/', or ','. |
fn |
An optional function name as a string. If provided, the operator expression is wrapped inside this function call. |
A data.table with one column containing the result of the evaluated expression.
## Not run:
library(data.table)
dtb <- data.table(a = 1:5, b = 6:10)
# Sum columns a and b
do_cols_dt(dtb, c("a", "b"), symbol = "+")
# Multiply columns a and b
do_cols_dt(dtb, c("a", "b"), symbol = "*")
# Apply the sum function to the columns
do_cols_dt(dtb, c("a", "b"), symbol = "+", fn = "sum")
## End(Not run)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.