setCols.DTSg: Set column values

setCols.DTSgR Documentation

Set column values

Description

Changes the values of columns, adds columns to and/or removes columns from a DTSg object. The values can optionally be set for certain rows only.

Usage

## S3 method for class 'DTSg'
setCols(
  x,
  i,
  cols = self$cols(class = "numeric")[1L],
  values,
  clone = getOption("DTSgClone"),
  ...
)

Arguments

x

A DTSg object (S3 method only).

i

An integerish vector indexing rows (positive numbers pick and negative numbers omit rows) or a filter expression accepted by the i argument of data.table::data.table. Filter expressions can contain the special symbol .N.

cols

A character vector specifying the columns whose values shall be set. Another possibility is a character string containing comma separated column names, for example, "x,y,z". The values of the .dateTime column cannot be changed.

values

A vector, list or list-like object (e.g. data.table::data.table) of replacement and/or new values accepted by the value argument of data.table's data.table::set function. NULL as a value removes a column.

clone

A logical specifying if the object shall be modified in place or if a deep clone (copy) shall be made beforehand.

...

Not used (S3 method only).

Value

Returns a DTSg object.

See Also

cols, getOption

Examples

# new DTSg object
x <- DTSg$new(values = flow)

# cap river flows to 100
## R6 method
x$setCols(
  i = flow > 100,
  cols = "flow",
  values = 100
)$print()

## 'set()' is a "hidden" R6 alias for 'setCols()'
x$set(
  i = flow > 100,
  cols = "flow",
  values = 100
)$print()

## S3 method
print(setCols(
  x = x,
  i = flow > 100,
  cols = "flow",
  values = 100
))

# set measurement unit with the help of 'units'
if (requireNamespace("units", quietly = TRUE)) {
  ## R6 method
  x$setCols(
    cols = "flow",
    values = units::set_units(x["flow"], "m^3/s")
  )$print()

  ## S3 method
  print(setCols(
    x = x,
    cols = "flow",
    values = units::set_units(x["flow"], "m^3/s")
  ))
}


DTSg documentation built on June 9, 2022, 1:06 a.m.