Rev 44236 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed
% File src/library/base/man/Extract.data.frame.Rd% Part of the R package, http://www.R-project.org% Copyright 1995-2007 R Core Development Team% Distributed under GPL 2 or later\name{Extract.data.frame}\alias{[.data.frame}\alias{[[.data.frame}\alias{[<-.data.frame}\alias{[[<-.data.frame}\alias{$<-.data.frame}\title{Extract or Replace Parts of a Data Frame}\description{Extract or replace subsets of data frames.}\usage{\method{[}{data.frame}(x, i, j, drop = )\method{[}{data.frame}(x, i, j) <- value\method{[[}{data.frame}(x, ..., exact = TRUE)\method{[[}{data.frame}(x, i, j) <- value\method{$}{data.frame}(x, i) <- value}\arguments{\item{x}{data frame.}\item{i, j, ...}{elements to extract or replace. For \code{[} and\code{[[}, these are \code{numeric} or \code{character} or, for\code{[} only, empty. Numeric values are coerced to integer as ifby \code{\link{as.integer}}. For replacement by \code{[}, a logicalmatrix is allowed. For replacement by \code{$}, \code{i} is a nameor literal character string.}\item{drop}{logical. If \code{TRUE} the result is coerced to thelowest possible dimension. The default is to drop if only onecolumn is left, but \bold{not} to drop if only one row is left.}\item{value}{A suitable replacement value: it will be repeated a wholenumber of times if necessary and it may be coerced: see theCoercion section. If \code{NULL}, deletes the column if a singlecolumn is selected.}\item{exact}{logical: see \code{\link{[}}, and applies to column names.}}\details{Data frames can be indexed in several modes. When \code{[} and\code{[[} are used with a single index (\code{x[i]} or \code{x[[i]]}),they index the data frame as if it were a list. In this usage a\code{drop} argument is ignored, with a warning. Using \code{$} isequivalent to using \code{[[i, exact = FALSE]]}.When \code{[} and \code{[[} are used with two indices (\code{x[i, j]}and \code{x[[i, j]]}) they act like indexing a matrix: \code{[[} canonly be used to select one element.If \code{[} returns a data frame it will have unique (and non-missing)row names, if necessary transforming the row names using\code{\link{make.unique}}. Similarly, column names will betransformed to be unique if necessary (e.g. if columns are selectedmore than once, or if more than one column of a given name is selectedif the data frame has duplicate columns).When \code{drop = TRUE}, this is applied to the subsetting of anymatrices contained in the data frame as well as to the data frame itself.The replacement methods can be used to add whole column(s) by specifyingnon-existent column(s), in which case the column(s) are added at theright-hand edge of the data frame and numerical indices must becontiguous to existing indices. On the other hand, rows can be addedat any row after the current last row, and the columns will bein-filled with missing values. Missing values in the indices are notallowed for replacement.For \code{[} the replacement value can be a list: each element of thelist is used to replace (part of) one column, recycling the list asnecessary. If columns specified by number are created, the names(if any) of the corresponding list elements are used to name thecolumns. If the replacement is not selecting rows, list values cancontain \code{NULL} elements which will cause the correspondingcolumns to be deleted. (See the Examples.)Matrix indexing using \code{[} is not recommended, and barelysupported. For extraction, \code{x} is first coerced to a matrix.For replacement a logical matrix (only) can be used to select theelements to be replaced in the same way as for a matrix.Both \code{[} and \code{[[} extraction methods partially match rownames. By default neither partially match column names, but\code{[[} will unless \code{exact=TRUE}. If you want to do exactmatching on row names use \code{\link{match}} as in the examples.}\section{Coercion}{The story over when replacement values are coerced is a complicatedone, and one that has changed during \R's development. This sectionis a guide only.When \code{[} and \code{[[} are used to add or replace a whole column,no coercion takes place but \code{value} will bereplicated (by calling the generic function \code{\link{rep}}) to theright length if an exact number of repeats can be used.When \code{[} is used with a logical matrix, each value is coerced tothe type of the column into which it is to be placed.When \code{[} and \code{[[} are used with two indices, thecolumn will be coerced as necessary to accommodate the value.Note that when the replacement value is an array (including a matrix)it is \emph{not} treated as a series of columns (as\code{\link{data.frame}} and \code{\link{as.data.frame}} do) butinserted as a single column.}\section{Warning}{The default behaviour when only one \emph{row} is left is equivalent tospecifying \code{drop = FALSE}. To drop from a data frame to a list,\code{drop = TRUE} has to be specified explicitly.}\value{For \code{[} a data frame, list or a single column (the latter twoonly when dimensions have been dropped). If matrix indexing is used forextraction a matrix results. If the result would be a data frame anerror results if undefined columns are selected (as there is no generalconcept of a 'missing' column in a data frame). Otherwise if a singlecolumn is selected and this is undefined the result is \code{NULL}.For \code{[[} a column of the data frame or \code{NULL}(extraction with one index)or a length-one vector (extraction with two indices).For \code{$}, a column of the data frame (or \code{NULL}).For \code{[<-}, \code{[[<-} and \code{$<-}, a data frame.}\seealso{\code{\link{subset}} which is often easier for extraction,\code{\link{data.frame}}, \code{\link{Extract}}.}\examples{sw <- swiss[1:5, 1:4] # select a manageable subsetsw[1:3] # select columnssw[, 1:3] # samesw[4:5, 1:3] # select rows and columnssw[1] # a one-column data framesw[, 1, drop = FALSE] # the samesw[, 1] # a (unnamed) vectorsw[[1]] # the samesw[1,] # a one-row data framesw[1,, drop=TRUE] # a listsw["C", ] # partially matchessw[match("C", row.names(sw)), ] # no exact match\dontshow{stopifnot(identical(sw[,1], sw[[1]]),identical(sw[,1][1], 80.2),identical(sw[,1, drop = FALSE], sw[1]),is.data.frame(sw[1]), dim(sw[1] ) == c(5,1),is.data.frame(sw[1,]),dim(sw[1,]) == c(1,4),is.list(s1 <- sw[1,, drop=TRUE]), identical(s1$Fertility, 80.2))}swiss[ c(1, 1:2), ] # duplicate row, unique row names are createdsw[sw <= 6] <- 6 # logical matrix indexingsw## adding a columnsw["new1"] <- LETTERS[1:5] # adds a character columnsw[["new2"]] <- letters[1:5] # dittosw[, "new3"] <- LETTERS[1:5] # dittosw$new4 <- 1:5sapply(sw, class)sw$new4 <- NULL # delete the columnswsw[6:8] <- list(letters[10:14], NULL, aa=1:5)# delete col7, update 6, appendsw## matrices in a data frameA <- data.frame(x=1:3, y=I(matrix(4:6)), z=I(matrix(letters[1:9],3,3)))A[1:3, "y"] # a matrixA[1:3, "z"] # a matrixA[, "y"] # a matrix}\keyword{array}