| Line 67... |
Line 67... |
| 67 |
in-filled with missing values. Missing values in the indices are not
|
67 |
in-filled with missing values. Missing values in the indices are not
|
| 68 |
allowed for replacement.
|
68 |
allowed for replacement.
|
| 69 |
|
69 |
|
| 70 |
For \code{[} the replacement value can be a list: each element of the
|
70 |
For \code{[} the replacement value can be a list: each element of the
|
| 71 |
list is used to replace (part of) one column, recycling the list as
|
71 |
list is used to replace (part of) one column, recycling the list as
|
| 72 |
necessary. If the columns specified by number are created, the names
|
72 |
necessary. If columns specified by number are created, the names
|
| 73 |
(if any) of the corresponding list elements are used to name the
|
73 |
(if any) of the corresponding list elements are used to name the
|
| 74 |
columns. If the replacement is not selecting rows, list values can
|
74 |
columns. If the replacement is not selecting rows, list values can
|
| 75 |
contain \code{NULL} elements which will cause the corresponding
|
75 |
contain \code{NULL} elements which will cause the corresponding
|
| 76 |
columns to be deleted.
|
76 |
columns to be deleted. (See the Examples.)
|
| 77 |
|
77 |
|
| 78 |
Matrixing indexing using \code{[} is not recommended, and barely
|
78 |
Matrixing indexing using \code{[} is not recommended, and barely
|
| 79 |
supported. For extraction, \code{x} is first coerced to a matrix.
|
79 |
supported. For extraction, \code{x} is first coerced to a matrix.
|
| 80 |
For replacement a logical matrix (only) can be used to select the
|
80 |
For replacement a logical matrix (only) can be used to select the
|
| 81 |
elements to be replaced in the same ways as for a matrix (except that
|
81 |
elements to be replaced in the same way as for a matrix.
|
| 82 |
missing values in the index are only allowed in a few special cases).
|
- |
|
| 83 |
}
|
82 |
}
|
| 84 |
\section{Coercion}{
|
83 |
\section{Coercion}{
|
| 85 |
The story over when replacement values are coerced is a complicated
|
84 |
The story over when replacement values are coerced is a complicated
|
| 86 |
one, and one that has changed during \R's development. This section
|
85 |
one, and one that has changed during \R's development. This section
|
| 87 |
is a guide only.
|
86 |
is a guide only.
|
| Line 143... |
Line 142... |
| 143 |
|
142 |
|
| 144 |
## adding a column
|
143 |
## adding a column
|
| 145 |
sw["new1"] <- LETTERS[1:5] # adds a character column
|
144 |
sw["new1"] <- LETTERS[1:5] # adds a character column
|
| 146 |
sw[["new2"]] <- letters[1:5] # ditto
|
145 |
sw[["new2"]] <- letters[1:5] # ditto
|
| 147 |
sw[, "new3"] <- LETTERS[1:5] # ditto
|
146 |
sw[, "new3"] <- LETTERS[1:5] # ditto
|
| 148 |
# but this got converted to a factor in 1.7.x
|
- |
|
| 149 |
sw$new4 <- 1:5
|
147 |
sw$new4 <- 1:5
|
| 150 |
sapply(sw, class)
|
148 |
sapply(sw, class)
|
| 151 |
sw$new4 <- NULL # delete the column
|
149 |
sw$new4 <- NULL # delete the column
|
| 152 |
sw
|
150 |
sw
|
| 153 |
sw[6:8] <- list(letters[10:14], NULL, aa=1:5) # delete col7, update 6, append
|
151 |
sw[6:8] <- list(letters[10:14], NULL, aa=1:5) # delete col7, update 6, append
|