The R Project SVN R

Rev

Rev 76103 | Rev 85065 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 76103 Rev 76118
Line 51... Line 51...
51
  \code{[[} are used with a single vector index (\code{x[i]} or
51
  \code{[[} are used with a single vector index (\code{x[i]} or
52
  \code{x[[i]]}), they index the data frame as if it were a list.  In
52
  \code{x[[i]]}), they index the data frame as if it were a list.  In
53
  this usage a \code{drop} argument is ignored, with a warning.
53
  this usage a \code{drop} argument is ignored, with a warning.
54
 
54
 
55
  There is no \code{data.frame} method for \code{$}, so \code{x$name}
55
  There is no \code{data.frame} method for \code{$}, so \code{x$name}
56
  uses the default method which treats \code{x} as a list (with no partial
56
  uses the default method which treats \code{x} as a list (with partial
-
 
57
  matching of column names if the match is unique, see
57
  matching of column names).  The replacement method (for \code{$}) checks
58
  \code{\link{Extract}}).  The replacement method (for \code{$}) checks
58
  \code{value} for the correct number of rows, and replicates it if necessary.
59
  \code{value} for the correct number of rows, and replicates it if necessary.
59
 
60
 
60
  When \code{[} and \code{[[} are used with two indices (\code{x[i, j]}
61
  When \code{[} and \code{[[} are used with two indices (\code{x[i, j]}
61
  and \code{x[[i, j]]}) they act like indexing a matrix:  \code{[[} can
62
  and \code{x[[i, j]]}) they act like indexing a matrix:  \code{[[} can
62
  only be used to select one element.  Note that for each selected
63
  only be used to select one element.  Note that for each selected
Line 161... Line 162...
161
sw[4:5, 1:3] # select rows and columns
162
sw[4:5, 1:3] # select rows and columns
162
sw[1]        # a one-column data frame
163
sw[1]        # a one-column data frame
163
sw[, 1, drop = FALSE]  # the same
164
sw[, 1, drop = FALSE]  # the same
164
sw[, 1]      # a (unnamed) vector
165
sw[, 1]      # a (unnamed) vector
165
sw[[1]]      # the same
166
sw[[1]]      # the same
-
 
167
sw$Fert      # the same (possibly w/ warning, see ?Extract)
166
 
168
 
167
sw[1,]       # a one-row data frame
169
sw[1,]       # a one-row data frame
168
sw[1,, drop = TRUE]  # a list
170
sw[1,, drop = TRUE]  # a list
169
 
171
 
170
sw["C", ] # partially matches
172
sw["C", ] # partially matches
Line 189... Line 191...
189
sw["new1"] <- LETTERS[1:5]   # adds a character column
191
sw["new1"] <- LETTERS[1:5]   # adds a character column
190
sw[["new2"]] <- letters[1:5] # ditto
192
sw[["new2"]] <- letters[1:5] # ditto
191
sw[, "new3"] <- LETTERS[1:5] # ditto
193
sw[, "new3"] <- LETTERS[1:5] # ditto
192
sw$new4 <- 1:5
194
sw$new4 <- 1:5
193
sapply(sw, class)
195
sapply(sw, class)
-
 
196
sw$new  # -> NULL: no unique partial match
194
sw$new4 <- NULL              # delete the column
197
sw$new4 <- NULL              # delete the column
195
sw
198
sw
196
sw[6:8] <- list(letters[10:14], NULL, aa = 1:5)
199
sw[6:8] <- list(letters[10:14], NULL, aa = 1:5)
197
# update col. 6, delete 7, append
200
# update col. 6, delete 7, append
198
sw
201
sw