Learn R Programming

ramify (version 0.1.0)

mat: Matrices

Description

Like matrix, mat creates a matrix from the given set of values. However, these values can also be represented by a character string, or a list of vectors.

Usage

mat(x, ...)

## S3 method for class 'default': mat(x, ...)

## S3 method for class 'character': mat(x, rows = TRUE, sep = ",", ...)

## S3 method for class 'list': mat(x, rows = TRUE, ...)

Arguments

x
A data vector, character string, or a list.
...
Aditional optional arguments.
rows
Logical. If TRUE (the default) the matrix is filled by rows, otherwise the matrix is filled by columns.
sep
Separator string. Values within each row/column of x are separated by this string. Default is ",".

See Also

bmat, dmat, matrix.

Examples

Run this code
## Using character vectors
mat('1, 2, 3, 4; 5, 6, 7, 8')  # ";" separates rows
mat('1, 2, 3, 4; 5, 6, 7, 8', rows = FALSE)  # ";" separates columns
mat("1 2 3 4; 5 6 7 8", sep = "")  # use spaces instead of commas
mat(c(1, 2, 3, 4, 5, 6, 7, 8), nrow = 2, byrow = TRUE)  # works like matrix too

## Using a list
z1 <- list(1:5, 6:10)
z2 <- list(a = 1:5, b = 6:10)
mat(z1)
mat(z2)  # preserves names as row names
mat(z2, rows = FALSE)  # preserves names as column names

Run the code above in your browser using DataLab