s-news
[Top] [All Lists]

Re: [S] stacking data.frame

To: ripley@stats.ox.ac.uk
Subject: Re: [S] stacking data.frame
From: Tim Hesterberg <THesterberg@statsci.com>
Date: Sat, 26 Feb 2000 09:49:04 -0800 (PST)
Cc: taam@Oakland.edu, s-news@wubios.wustl.edu
In-reply-to: <Pine.GSO.4.05.10002231601090.3650-100000@auk.stats> (message from Prof Brian D Ripley on Wed, 23 Feb 2000 16:04:16 +0000 (GMT))
References: <Pine.GSO.4.05.10002231601090.3650-100000@auk.stats>
Reply-to: THesterberg@statsci.com (Tim Hesterberg)
Sender: owner-s-news@wubios.wustl.edu
>On Wed, 23 Feb 2000, Winson Taam wrote:
>> 
>> tmp is a data.frame with elements
>> ...
>> I want to replicate each row 3 times. i.e.

Brian Ripley wrote:
>nr <- nrow(tmp)
>tmp[rep(1:nr, rep(3, nr)), , drop=FALSE]

Equivalently (but simpler):
        tmp[rep(1:nr, each=3), , drop=FALSE]

The drop=F part is necessary if tmp had a single column.


>Note that row names in data frames are supposed to be unique, and if
>you want to replicate them, make them into another column first.

Alternately, set the dup.row.names attribute to non-NULL, e.g. one of:
        attr(tmp, "dup.row.names") <- T
        myNewDataFrame <- data.frame(..., dup.row.names=T)

It used to be the case that row names had to be unique, and there is
probably code around that expects unique names, so be cautious about
using this.

Conversely, if you write new code, you should not expect that row
names are unique.  For example, the current version of the S-PLUS bootstrap()
creates data frames with duplicated row names if the `data' argument
is a data frame.  This is done for speed, because checking for and
creating unique row names is slow, particularly if there are many
rows.

Tim Hesterberg

========================================================
| Tim Hesterberg       THesterberg@splus.mathsoft.com  |
| Research Scientist   MathSoft, Inc.                  |
| (206)283-8802x319    1700 Westlake Ave. N, Suite 500 |
| (206)283-0347 (fax)  Seattle, WA 98109-3044, U.S.A.  |
|                      www.statsci.com/Hesterberg      |
========================================================
-----------------------------------------------------------------------
This message was distributed by s-news@wubios.wustl.edu.  To unsubscribe
send e-mail to s-news-request@wubios.wustl.edu with the BODY of the
message:  unsubscribe s-news

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