left: Return the leftmost or rightmost columns of a matrix or data...

View source: R/left.R

leftR Documentation

Return the leftmost or rightmost columns of a matrix or data frame

Description

Return the leftmost or rightmost or columns of a matrix or data frame

Usage

right(x, n = 6L, ...)
left(x, n=6L, ...)

## S3 method for class 'matrix'
right(x, n=6L, add.col.nums=TRUE, ...)
## S3 method for class 'matrix'
left(x, n=6L, add.col.nums=TRUE, ...)

## S3 method for class 'data.frame'
right(x, n=6L, add.col.nums=TRUE, ...)
## S3 method for class 'data.frame'
left(x, n=6L, add.col.nums=TRUE, ...)

Arguments

x

Matrix or data frame

n

If positive, number of columns to return. If negative, number of columns to omit. See examples.

add.col.nums

Logical. If no column names are present, add names giving original column number. (See example below.)

...

Additional arguments used by methods

Value

An object consisting of the leftmost or rightmost n columns of x.

Author(s)

Gregory R. Warnes greg@warnes.net

See Also

first, last, head, tail

Examples

m <- matrix(1:100, ncol=10)
colnames(m) <- paste("Col",1:10, sep="_")

left(m)
right(m)

# When no column names are present, they are added by default
colnames(m) <- NULL

left(m)
colnames(left(m))

right(m)
colnames(right(m))

# Prevent addition of column numbers
left(m, add.col.nums = FALSE)
colnames(left(m, add.col.nums = FALSE))

right(m, add.col.nums = FALSE)            # columns are labeled 1:6
colnames(right(m, add.col.nums = FALSE))  #   instead of 5:10

# Works for data frames too!
d <- data.frame(m)
left(d)
right(d)

# Use negative n to specify number of columns to omit
left(d, -3)
right(d, -3)

gdata documentation built on Oct. 17, 2023, 1:11 a.m.