Learn R Programming

lsr (version 1.0.0)

copy: Copy a vector into a matrix

Description

Creates a matrix by stacking multiple copies of a vector as rows (rowCopy) or as columns (colCopy).

Usage

colCopy(x, times, dimnames = NULL)

rowCopy(x, times, dimnames = NULL)

Value

For rowCopy, a matrix with times rows and length(x) columns, where each row is x. For colCopy, a matrix with length(x) rows and times columns, where each column is x.

Arguments

x

A vector to be copied.

times

The number of copies to stack together.

dimnames

An optional list specifying row and column names for the output matrix.

Details

These functions are shortcuts for building a matrix where every row (or every column) is the same vector. They are equivalent to calling matrix with appropriate byrow and dimension arguments.

See Also

Examples

Run this code
x <- c(3, 1, 4, 1, 5)

# stack x as rows
rowCopy(x, 4)

# stack x as columns
colCopy(x, 4)

# with custom dimension names
dnames <- list(rows = c("r1", "r2", "r3"), cols = c("c1", "c2", "c3", "c4", "c5"))
rowCopy(x, 3, dnames)

Run the code above in your browser using DataLab