s-news
[Top] [All Lists]

Re: collapse to unique row.names using mean()

To: "Phillip Staford" <biomining@hotmail.com>, s-news@lists.biostat.wustl.edu
Subject: Re: collapse to unique row.names using mean()
From: John Fox <jfox@mcmaster.ca>
Date: Sun, 19 Jan 2003 09:59:17 -0500
In-reply-to: <F15v442ryquzIBB6J4E0000ec2a@hotmail.com>
Dear Phillip,

Your example suggests that the rows with duplicate names are contiguous. If this is the case, then the following should do what you want without using a lot of memory (where df is the data frame):

rows <- c("",row.names(df))
keep <- 1:nrow(df)
for (i in 1:nrow(df)) if (rows[i+1] == rows[i]) keep[i] <- 0
df <- df[keep,]

I hope that this helps,
 John

At 12:33 AM 1/19/2003 -0700, Phillip Staford wrote:
Dear S group;

I'm looking for a very low-memory (slow is OK, but memory is critical here) method to collapse a data.frame that has a mixture of unique and replicated row.names. What's the best way to do this without burning unnecessary memory?
A  3
A  2
A  1
B  3
C  2
C  4
into
A  2
B  3
C  3


Thanks for any suggestions, the obvious ways are quick but not memory efficient I think.

-----------------------------------------------------
John Fox
Department of Sociology
McMaster University
Hamilton, Ontario, Canada L8S 4M4
email: jfox@mcmaster.ca
phone: 905-525-9140x23604
web: www.socsci.mcmaster.ca/jfox
-----------------------------------------------------


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