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").')
}
Run the code above in your browser using DataCamp Workspace