setter {setter} | R Documentation |
Set attributes of a variable, then return that variable.
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)
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. |
x
is returned, with updated attributes.
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").') }