Skip to contents

Functions to rename a supported mutable object using pass-by-reference semantics:

  • sb_setFlatnames() renames the (flat) names of a mutable_atomic object.

  • sb_setDimnames() renames the dimension names of a mutable_atomic object.

  • sb2_setVarnames() renames the variable names of a data.table object.

Usage

sb_setFlatnames(x, i = NULL, newnames, ...)

sb_setDimnames(x, m, newdimnames, ...)

sb2_setVarnames(x, old, new, skip_absent = FALSE, ...)

Arguments

x

a variable belonging to one of the supported mutable classes.

i

logical, numeric, character, or imaginary indices, indicating which flatnames should be changed.
If i = NULL, the names will be completely replaced.

newnames

Atomic character vector giving the new names.
Specifying NULL will remove the names.

...

see squarebrackets_method_dispatch.

m

integer vector giving the margin(s) for which to change the names (m = 1L for rows, m = 2L for columns, etc.).

newdimnames

a list of the same length as m.
The first element of the list corresponds to margin m[1], the second element to m[2], and so on.
The components of the list can be either NULL, or a character vector with the same length as the corresponding dimension.
Instead of a list, simply NULL can be specified, which will remove the dimnames completely.

old

the old column names

new

the new column names, in the same order as old

skip_absent

Skip items in old that are missing (i.e. absent) in names(x).
Default FALSE halts with error if any are missing.

Value

Returns: VOID. This method modifies the object by reference.

Do not use assignment like names(x) <- sb_setRename(x, ...).

Since this function returns void, you'll just get NULL.


Examples

 
# mutable atomic vector ====
x <- y <- mutable_atomic(1:10, names = letters[1:10])
print(x)
#>  a  b  c  d  e  f  g  h  i  j 
#>  1  2  3  4  5  6  7  8  9 10 
#> mutable_atomic 
#> typeof:  integer 
sb_setFlatnames(x, newnames = rev(letters[1:10]))
print(y)
#>  j  i  h  g  f  e  d  c  b  a 
#>  1  2  3  4  5  6  7  8  9 10 
#> mutable_atomic 
#> typeof:  integer 

x <- y <- mutable_atomic(1:10, names = letters[1:10])
print(x)
#>  a  b  c  d  e  f  g  h  i  j 
#>  1  2  3  4  5  6  7  8  9 10 
#> mutable_atomic 
#> typeof:  integer 
sb_setFlatnames(x, 1L, "XXX")
print(y)
#> XXX   b   c   d   e   f   g   h   i   j 
#>   1   2   3   4   5   6   7   8   9  10 
#> mutable_atomic 
#> typeof:  integer 

################################################################################


# mutable atomic matrix ====
x <- mutable_atomic(
  1:20, dim = c(5, 4), dimnames = n(letters[1:5], letters[1:4])
)
print(x)
#>   a  b  c  d
#> a 1  6 11 16
#> b 2  7 12 17
#> c 3  8 13 18
#> d 4  9 14 19
#> e 5 10 15 20
#> mutable_atomic 
#> typeof:  integer 
sb_setDimnames(
  x,
  1:2,
  lapply(dimnames(x), rev)
)
print(x)
#>   d  c  b  a
#> e 1  6 11 16
#> d 2  7 12 17
#> c 3  8 13 18
#> b 4  9 14 19
#> a 5 10 15 20
#> mutable_atomic 
#> typeof:  integer 



################################################################################



# data.table ====

x <- data.table::data.table(
  a = 1:20,
  b = letters[1:20]
)
print(x)
#>         a      b
#>     <int> <char>
#>  1:     1      a
#>  2:     2      b
#>  3:     3      c
#>  4:     4      d
#>  5:     5      e
#>  6:     6      f
#>  7:     7      g
#>  8:     8      h
#>  9:     9      i
#> 10:    10      j
#> 11:    11      k
#> 12:    12      l
#> 13:    13      m
#> 14:    14      n
#> 15:    15      o
#> 16:    16      p
#> 17:    17      q
#> 18:    18      r
#> 19:    19      s
#> 20:    20      t
#>         a      b
sb2_setVarnames(x, old = names(x), new = rev(names(x)))
print(x)
#>         b      a
#>     <int> <char>
#>  1:     1      a
#>  2:     2      b
#>  3:     3      c
#>  4:     4      d
#>  5:     5      e
#>  6:     6      f
#>  7:     7      g
#>  8:     8      h
#>  9:     9      i
#> 10:    10      j
#> 11:    11      k
#> 12:    12      l
#> 13:    13      m
#> 14:    14      n
#> 15:    15      o
#> 16:    16      p
#> 17:    17      q
#> 18:    18      r
#> 19:    19      s
#> 20:    20      t
#>         b      a