Set attributes of variables

Share:

Description

Set attributes of a variable, then return that variable.

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
set_class(x, value)

copy_class(x, from)

set_mode(x, value)

copy_mode(x, from)

set_storage_mode(x, value)

copy_storage_mode(x, from)

set_dim(x, value)

copy_dim(x, from)

set_length(x, value)

copy_length(x, from)

set_names(x, value)

copy_names(x, from)

set_colnames(x, value)

copy_colnames(x, from)

set_rownames(x, value)

copy_rownames(x, from)

set_dimnames(x, value)

copy_dimnames(x, from)

set_levels(x, value)

copy_levels(x, from)

set_comment(x, value)

copy_comment(x, from)

set_attributes(x, ..., .dots = list())

copy_attributes(x, from, attribs)

copy_all_attributes(x, from)

copy_most_attributes(x, from)

Arguments

x

An R variable.

value

A value to set the attribute to.

from

A variable to take the attribute value from.

...

Name-value pairs of attributes to set.

.dots

A named list of attributes to set.

attribs

A character vector of attributes to copy.

Value

x is returned, with updated attributes.

See Also

attributes, attr

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
if(requireNamespace("magrittr"))
{
  `%>%` <- magrittr::`%>%`
  # Convert a vector to a matrix by setting the dimensions and their names.
  m <- 1:12 %>%
    set_dim(3:4) %>%
    set_dimnames(list(letters[1:3], LETTERS[1:4])) %>%
    print

  # Copy attributes from one variable to another using copy_* fns.
  month.abb %>%
    copy_dim(m) %>%
    copy_dimnames(m) %>%
    print

  # Same again, using copy_attributes
  month.abb %>%
    copy_attributes(m, c("dim", "dimnames"))

  # Same again, in this case you can copy most/all the attributes from m.
  month.abb %>%
    copy_most_attributes(m)
  month.abb %>%
    copy_all_attributes(m)

  # To quickly convert a list into a data.frame, set the class and row names.
  list(a = (1:5) ^ 2, b = pi ^ (1:5)) %>%
    set_class("data.frame") %>%
    set_rownames() %>%   # data.frames have a default
    print

  # Or equivalently, using attributes
  list(a = (1:5) ^ 2, b = pi ^ (1:5)) %>%
    set_attributes(class = "data.frame", row.names = .set_row_names(5)) %>%
    print
} else
{
  message('This example requires the magrittr package.  Please run install.packages("magrittr").')
}

Want to suggest features or report bugs for rdrr.io? Use the GitHub issue tracker.