Learn R Programming

tidyfst (version 0.9.3)

set_dt: Fast operations of data.table by reference (I)

Description

Combination of set* functions provided by data.table. This is memeroy efficient because no copy is made at all.

Usage

set_dt(
  data,
  col_order = NULL,
  order_by = NULL,
  order_dirc = 1L,
  old_name = NULL,
  new_name = NULL,
  fill_cols = NULL,
  fill_type = "down",
  fill_value = NA
)

Arguments

data

A data.frame

col_order

(Optional) Character vector of the new column name ordering. May also be column numbers. This parameter will pass to "neworder" parameter in setcolorder.

order_by

(Optional) A character vector of column names by which to order. This parameter will pass to "cols" parameter in setorder.

order_dirc

(Optional) An integer vector with only possible values of 1 and -1, corresponding to ascending and descending order. This parameter will pass to "order" parameter in setorder. Default uses 1.

old_name

(Optional) When new_name is provided, character names or numeric positions of column names to change. This parameter will pass to "old" parameter in setnames.

new_name

(Optional) It can be a function or the new column names. This parameter will pass to "new" parameter in setnames.

fill_cols

(Optional) Numeric or character vector specifying columns to be updated.

fill_type

(Optional) Character, one of "down", "up" or "replace". Defaults to "down".

fill_value

(Optional) Numeric or integer, value to be used to fill when fill_type=="replace". Defaults to NA.

Value

The input is modified by reference, and returned (invisibly) so it can be used in compound statements.

Details

The set_dt() will first set any data.frame to a data.table, then rename, fill NAs, arrange row order, arrange column order. If you want to do the operation in another order, use it separately in multiple set_dt functions in the desired order.

See Also

setcolorder, setorder, setnames, setnafill

set_in_dt

Examples

Run this code
# NOT RUN {
# set_dt
x = 1:10
x[c(1:2, 5:6, 9:10)] = NA
dt = data.table(v1=x, v2=lag_dt(x)/2, v3=lead_dt(x, 1L)/2)
dt
set_dt(dt,new_name = c("A","B","C"),fill_cols = names(dt),
        order_by = "A",order_dirc = -1,col_order = c("B","A","C"))
dt

# }

Run the code above in your browser using DataLab