s-news
[Top] [All Lists]

Re: dimnames in Sparc Splus 6.0

To: Gary Sabot <gary@sabot.com>
Subject: Re: dimnames in Sparc Splus 6.0
From: Tim Hesterberg x319 <timh@insightful.com>
Date: Wed, 5 Sep 2001 14:01:12 -0700
Cc: s-news@lists.biostat.wustl.edu, therneau@Mayo.EDU (Terry Therneau)
In-reply-to: <15254.22373.791947.168132@delphioutpost.com> (message from Gary Sabot on Wed, 5 Sep 2001 12:48:37 -0400)
References: <15253.31719.88959.727681@delphioutpost.com> <15254.22373.791947.168132@delphioutpost.com>
Reply-to: timh@insightful.com (Tim Hesterberg)
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

<Prev in Thread] Current Thread [Next in Thread>