Combination of set*
functions provided by data.table.
This is memeroy efficient because no copy is made at all.
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 )
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 |
order_by | (Optional) A character vector of column names by which to order.
This parameter will pass to "cols" parameter in |
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 |
old_name | (Optional) When |
new_name | (Optional) It can be a function or the new column names.
This parameter will pass to "new" parameter in
|
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
|
The input is modified by reference, and returned (invisibly) so it can be used in compound statements.
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.
# 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#> v1 v2 v3 #> <int> <num> <num> #> 1: NA NA NA #> 2: NA NA 1.5 #> 3: 3 NA 2.0 #> 4: 4 1.5 NA #> 5: NA 2.0 NA #> 6: NA NA 3.5 #> 7: 7 NA 4.0 #> 8: 8 3.5 NA #> 9: NA 4.0 NA #> 10: NA NA NAset_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#> B A C #> <num> <int> <num> #> 1: NA NA NA #> 2: NA NA 1.5 #> 3: 3.5 8 4.0 #> 4: 4.0 8 4.0 #> 5: 4.0 8 4.0 #> 6: 2.0 7 4.0 #> 7: 1.5 4 2.0 #> 8: 2.0 4 2.0 #> 9: 2.0 4 3.5 #> 10: NA 3 2.0