Gary Sabot just posted a version of [.data.frame for which
X[,aSingleColumn] # where X is a data frame
adds the row names of the data frame as names to the vector
that is returned.
Unfortunately, that version of [.data.frame makes assumptions about
the type of data included in data frames that are often unjustified.
This causes it to mess up with some kinds of data, including:
* factors (e.g. try fuel.frame[,"Type"] with and without that version)
* objects with new-style classes. This makes it incompatible with
with library("missing") (a library in S-PLUS 6.0 for handling
missing data using multiple imputations).
More generally, adding names to vectors is often undesirable; it may
* substantially increase the size of the resulting object,
* slow down some S-PLUS computations, and
* cause bugs for code that expects data without names; also
* there is no way to determine which variables in a data frame had
names originally. So names may be added to variables that should
not have them, or which are incorrect for the variable.
It is safer not to mask the default version of [.data.frame,
but rather to use a function like the following to extract a single
variable from a data frame and add names to it:
extractVariableAddNames <- function(df, j){
# df is a data frame, j a column to subscript.
# Return df[,j], but with df's row names added.
x <- df[,j]
if(is.data.frame(x))
stop("Subscripted more than one column")
names(x) <- row.names(df)
x
}
========================================================
| Tim Hesterberg Research Scientist |
| timh@insightful.com Insightful Corp. |
| (206)283-8802x319 1700 Westlake Ave. N, Suite 500 |
| (206)283-6310 (fax) Seattle, WA 98109-3044, U.S.A. |
========================================================
Formerly known as MathSoft, Insightful Corporation provides analytical
solutions leveraging S-PLUS, StatServer, and Consulting services
|