left | R Documentation |
Return the leftmost or rightmost or columns of a matrix or data frame
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, ...)
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 |
An object consisting of the leftmost or rightmost n
columns
of x
.
Gregory R. Warnes greg@warnes.net
first
,
last
,
head
,
tail
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)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.