Learn R Programming

gdata (version 2.19.0)

left: Return the leftmost or rightmost columns of a matrix or dataframe

Description

Return the leftmost or rightmost or columns of a matrix or dataframe

Usage

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

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

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

Value

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

Arguments

x

Matrix or dataframe

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

Author

Gregory R. Warnes greg@warnes.net

See Also

first, last, head, tail

Examples

Run this code
 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)

Run the code above in your browser using DataLab