The R Project SVN R

Rev

Rev 27625 | Rev 31067 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
24767 ripley 1
\name{Extract.data.frame}
2
\alias{[.data.frame}
3
\alias{[[.data.frame}
4
\alias{[<-.data.frame}
5
\alias{[[<-.data.frame}
24878 ripley 6
\alias{$<-.data.frame}
24767 ripley 7
\title{Extract or Replace Parts of a Data Frame}
8
\description{
9
  Extract or replace subsets of data frames.
10
}
11
\usage{
12
x[i]
13
x[i] <- value
14
x[i, j, drop = TRUE]
15
x[i, j] <- value
16
 
17
x[[i]]
18
x[[i]] <- value
19
x[[i, j]]
20
x[[i, j]] <- value
24878 ripley 21
 
22
x$name
23
x$name <- value
24767 ripley 24
}
25
\arguments{
26
  \item{x}{data frame.}
27
  \item{i, j}{elements to extract or replace. \code{i, j} are
28
    \code{numeric} or \code{character} or, for \code{[} only, empty.
29
    Numeric values are coerced to integer as if by \code{\link{as.integer}}.
30
    For replacement by \code{[}, a logical matrix is allowed.
31
  }
24855 ripley 32
 
24767 ripley 33
  \item{drop}{logical.  If \code{TRUE} the result is coerced to the
34
    lowest possible dimension: however, see the Warning below.}
24855 ripley 35
 
24767 ripley 36
  \item{value}{A suitable replacement value: it will be repeated a whole
24811 ripley 37
    number of times if necessary and it may be coerced: see the
24878 ripley 38
    Coercion section.  If \code{NULL}, deletes the column if a single
39
    column is selected.}
40
 
25543 hornik 41
  \item{name}{name or literal character string.}
24767 ripley 42
}
43
\details{
44
  Data frames can be indexed in several modes.  When \code{[} and
45
  \code{[[} are used with a single index, they index the data frame
24878 ripley 46
  as if it were a list.  In this usage a \code{drop} argument is
47
  ignored, with a warning.  Using \code{$} is equivalent to using
24889 ripley 48
  \code{[[} with a single index.
24767 ripley 49
 
50
  When \code{[} and \code{[[} are used with two indices they act
51
  like indexing a matrix:  \code{[[} can only be used to select one element.
52
 
24855 ripley 53
  If \code{[} returns a data frame it will have unique (and non-missing)
54
  row names, if necessary transforming the row names using
24974 ripley 55
  \code{\link{make.unique}}.  Similarly, column names
24855 ripley 56
  will be transformed (if columns are selected more than once).
24767 ripley 57
 
24855 ripley 58
  When \code{drop =TRUE}, this is applied to the subsetting of any
59
  matrices contained in the data frame as well as to the data frame itself.
60
 
24767 ripley 61
  The replacement methods can be used to add whole column(s) by specifying
62
  non-existent column(s), in which case the column(s) are added at the
63
  right-hand edge of the data frame and numerical indices must be
24855 ripley 64
  contiguous to existing indices.  On the other hand, rows can be added
65
  at any row after the current last row, and the columns will be
66
  in-filled with missing values.
24767 ripley 67
 
24889 ripley 68
  For \code{[} the replacement value can be a list: each element of the
69
  list is used to replace (part of) one column, recycling the list as
70
  necessary.  If the columns specified by number are created, the names
71
  (if any) of the corresponding list elements are used to name the
27625 ripley 72
  columns.  If the replacement is not selecting rows, list values can
24889 ripley 73
  contain \code{NULL} elements which will cause the corresponding
74
  columns to be deleted.
75
 
24855 ripley 76
  Matrixing indexing using \code{[} is not recommended, and barely
77
  supported.  For extraction, \code{x} is first coerced to a matrix.
78
  For replacement a logical matrix (only) can be used to select the
79
  elements to be replaced in the same ways as for a matrix.
80
  Missing values in the matrix are treated as false, unlike S which
81
  does not replace them but uses up the corresponding values in \code{value}.
24767 ripley 82
}
24811 ripley 83
\section{Coercion}{
84
  The story over when replacement values are coerced is a complicated
85
  one, and one that has changed during \R's development.  This section
24825 ripley 86
  is a guide only.
24811 ripley 87
 
24855 ripley 88
  When \code{[} and \code{[[} are used to add or replace a whole column,
89
  no coercion takes place but \code{value} will be
24825 ripley 90
  replicated (by calling the generic function \code{\link{rep}}) to the
91
  right length if an exact number of repeats can be used.
24811 ripley 92
 
93
  When \code{[} is used with a logical matrix, each value is coerced to
94
  the type of the column in which it is to be placed.
95
 
24855 ripley 96
  When  \code{[} and \code{[[} are used with two indices, the
24889 ripley 97
  column will be coerced as necessary to accommodate the value.
27928 ripley 98
 
99
  Note that when the replacement value is an array (including a matrix)
100
  it is \emph{not} treated as a series of columns (as
101
  \code{\link{data.frame}} and \code{\link{as.data.frame}} do) but
102
  inserted as a single column.
24811 ripley 103
}
24767 ripley 104
\section{Warning}{
105
  Although the default for \code{drop} is \code{TRUE}, the default
24889 ripley 106
  behaviour  when only one \emph{row} is left is equivalent to
107
  specifying \code{drop = FALSE}.  To drop from a data frame to a list,
108
  \code{drop = FALSE} has to specified explicitly.
24767 ripley 109
}
110
\value{
24855 ripley 111
  For \code{[} a data frame, list or a single column (the latter two
112
  only when dimensions have been dropped).  If matrix indexing is used for
113
  extraction a matrix results.
24767 ripley 114
 
24855 ripley 115
  For \code{[[} a column of the data frame (extraction with one index)
116
  or a length-one vector (extraction with two indices).
117
 
24889 ripley 118
  For \code{[<-}, \code{[[<-} and \code{$<-}, a data frame.
24767 ripley 119
}
120
\seealso{
25898 maechler 121
  \code{\link{subset}} which is often easier for extraction,
122
  \code{\link{data.frame}}, \code{\link{Extract}}.
24767 ripley 123
}
124
\examples{
125
data(swiss)
126
sw <- swiss[1:5, 1:4]  # select a manageable subset
127
 
128
sw[1:3]      # select columns
129
sw[, 1:3]    # same
130
sw[4:5, 1:3] # select rows and columns
131
sw[1]        # a one-column data frame
132
sw[, 1, drop = FALSE]  # the same
27625 ripley 133
sw[, 1]      # a (unnamed) vector
24767 ripley 134
sw[[1]]      # the same
135
 
136
sw[1,]       # a one-row data frame
137
sw[1,, drop=TRUE]  # a list
138
 
139
swiss[ c(1, 1:2), ]   # duplicate row, unique row names are created
140
 
141
sw[sw <= 6] <- 6  # logical matrix indexing
142
sw
24811 ripley 143
 
144
## adding a column
145
sw["new1"] <- LETTERS[1:5]   # adds a character column
146
sw[["new2"]] <- letters[1:5] # ditto
24855 ripley 147
sw[, "new3"] <- LETTERS[1:5] # ditto
148
                             # but this got converted to a factor in 1.7.x
24878 ripley 149
sw$new4 <- 1:5
24811 ripley 150
sapply(sw, class)
24878 ripley 151
sw$new4 <- NULL              # delete the column
24889 ripley 152
sw
153
sw[6:8] <- list(letters[10:14], NULL, aa=1:5) # delete col7, update 6, append
154
sw
24811 ripley 155
 
24855 ripley 156
## matrices in a data frame
157
A <- data.frame(x=1:3, y=I(matrix(4:6)), z=I(matrix(letters[1:9],3,3)))
158
A[1:3, "y"] # a matrix, was a vector prior to 1.8.0
25898 maechler 159
A[1:3, "z"] # a matrix
24855 ripley 160
A[, "y"]    # a matrix
161
}
24767 ripley 162
\keyword{array}