Hello, I have a data frame that has three fields. Resp# ActCode ProdUsed 100 3 2 100 3 2 100 4 3 100 4 3 101 3 6 102 2 1 102 3 1 103 5 1 103 5 1 103 3 2 103 3 2 104 3 1 What I seek to do. If a row following a row is identical for the fields Resp3 and ActCode, I then want to delete one of the two matching rows. Based on this logic, the resulting df would look like that shown below. Resp# ActCode ProdUsed 100 3 2 100 4 3 101 3 6 102 2 1 102 3 1 103 5 1 103 3 2 104 3 1 I have tried match, tried to write something that if the current row minus the previous row equal 0 for both Resp# and ActCode, then delete the row, but to no avail. Not knowing what to search on for this problem, I turn to the R experts for help. (Windows 2000, R 2.0, 384 meg memory) Greg Blevins The Market Solutions Group
Greg Blevins wrote:> Hello, > > I have a data frame that has three fields. > > Resp# ActCode ProdUsed > 100 3 2 > 100 3 2 > 100 4 3 > 100 4 3 > 101 3 6 > 102 2 1 > 102 3 1 > 103 5 1 > 103 5 1 > 103 3 2 > 103 3 2 > 104 3 1 > > What I seek to do. > > If a row following a row is identical for the fields Resp3 and ActCode, I > then want to delete one of the two matching rows. Based on this logic, the > resulting df would look like that shown below. > > Resp# ActCode ProdUsed > 100 3 2 > 100 4 3 > 101 3 6 > 102 2 1 > 102 3 1 > 103 5 1 > 103 3 2 > 104 3 1 > > I have tried match, tried to write something that if the current row minus > the previous row equal 0 for both Resp# and ActCode, then delete the row, > but to no avail. Not knowing what to search on for this problem, I turn to > the R experts for help. > > (Windows 2000, R 2.0, 384 meg memory) > Greg Blevins > The Market Solutions GroupWould ?unique work for you? > x Resp ActCode ProdUsed 1 100 3 2 2 100 3 2 3 100 4 3 4 100 4 3 5 101 3 6 6 102 2 1 7 102 3 1 8 103 5 1 9 103 5 1 10 103 3 2 11 103 3 2 12 104 3 1 > unique(x) Resp ActCode ProdUsed 1 100 3 2 3 100 4 3 5 101 3 6 6 102 2 1 7 102 3 1 8 103 5 1 10 103 3 2 12 104 3 1 Or if you only want to keep the unique rows of the first two columns then: > x[!duplicated(x[, 1:2]), ] which for this example is identical to using unique directly. --sundar
On Wed, 2004-10-06 at 16:26, Greg Blevins wrote:> Hello, > > I have a data frame that has three fields. > > Resp# ActCode ProdUsed > 100 3 2 > 100 3 2 > 100 4 3 > 100 4 3 > 101 3 6 > 102 2 1 > 102 3 1 > 103 5 1 > 103 5 1 > 103 3 2 > 103 3 2 > 104 3 1 > > What I seek to do. > > If a row following a row is identical for the fields Resp3 and ActCode, I > then want to delete one of the two matching rows. Based on this logic, the > resulting df would look like that shown below.I'm sure that the Rexperts will provide more elegant solutions but heres a way which works assuming columns 2 & 3 are numeric so that diff() works. If d is the data.frame> dResp ActCode ProdUsed 1 100 3 2 2 100 3 2 3 100 4 3 4 100 4 3 5 101 3 6 6 102 2 1 7 102 3 1 8 103 5 1 9 103 5 1 10 103 3 2 11 103 3 2 12 104 3 1> > y <- which( diff(d[,2]) == 0 & diff(d[,3]) == 0 ) > d[-y,]Resp ActCode ProdUsed 2 100 3 2 4 100 4 3 5 101 3 6 6 102 2 1 7 102 3 1 9 103 5 1 11 103 3 2 12 104 3 1> Resp# ActCode ProdUsed > 100 3 2 > 100 4 3 > 101 3 6 > 102 2 1 > 102 3 1 > 103 5 1 > 103 3 2 > 104 3 1HTH ------------------------------------------------------------------- Rajarshi Guha <rxg218 at psu.edu> <http://jijo.cjb.net> GPG Fingerprint: 0CCA 8EE2 2EEB 25E2 AB04 06F7 1BB9 E634 9B87 56EE ------------------------------------------------------------------- After a number of decimal places, nobody gives a damn.
Thanks to all that responded to my problem. I am always amazed by how helpful the R community is! Greg Blevins [[alternative HTML version deleted]]
Dear Greg, How about this?> n <- nrow(Data) > index <- 1:n > sel <- c(TRUE, !apply(Data[index[-1], c(1,2)] =+ Data[index[-n], c(1,2)], 1, all)) > Data[sel,]Resp ActCode ProdUsed 1 100 3 2 3 100 4 3 5 101 3 6 6 102 2 1 7 102 3 1 8 103 5 1 10 103 3 2 12 104 3 1 I hope this helps, John On Wed, 6 Oct 2004 15:26:34 -0500 "Greg Blevins" <gblevins at mn.rr.com> wrote:> Hello, > > I have a data frame that has three fields. > > Resp# ActCode ProdUsed > 100 3 2 > 100 3 2 > 100 4 3 > 100 4 3 > 101 3 6 > 102 2 1 > 102 3 1 > 103 5 1 > 103 5 1 > 103 3 2 > 103 3 2 > 104 3 1 > > What I seek to do. > > If a row following a row is identical for the fields Resp3 and > ActCode, I > then want to delete one of the two matching rows. Based on this > logic, the > resulting df would look like that shown below. > > Resp# ActCode ProdUsed > 100 3 2 > 100 4 3 > 101 3 6 > 102 2 1 > 102 3 1 > 103 5 1 > 103 3 2 > 104 3 1 > > I have tried match, tried to write something that if the current row > minus > the previous row equal 0 for both Resp# and ActCode, then delete the > row, > but to no avail. Not knowing what to search on for this problem, I > turn to > the R experts for help. > > (Windows 2000, R 2.0, 384 meg memory) > Greg Blevins > The Market Solutions Group > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html-------------------------------- John Fox Department of Sociology McMaster University Hamilton, Ontario, Canada http://socserv.mcmaster.ca/jfox/
as.complex("2+1i") -> 2+1i as.complex("2+i") -> NA Does somebody have a modified version of as.complex which does the coercion in a less strict manner and produces a complex number also for strings like the second example? Perhaps it would even make sense to change the behavior of as.complex to handle the second case. After all, this is an established way of writing complex numbers in some programming tools able to deal with complex numbers. -- Erich Neuwirth, Computer Supported Didactics Working Group Visit our SunSITE at http://sunsite.univie.ac.at Phone: +43-1-4277-38624 Fax: +43-1-4277-9386
On 06-Oct-04 Greg Blevins wrote:> Thanks to all that responded to my problem. I am always amazed by how > helpful the R community is!Because the R community, via the R list, like any good mailing list community, is like a global coffee-room where anyone can start talking about what concerns them at the time, and other people looking over their shoulder, or over-hearing, can come in with their view of the matter. Ted. -------------------------------------------------------------------- E-Mail: (Ted Harding) <Ted.Harding at nessie.mcc.ac.uk> Fax-to-email: +44 (0)870 094 0861 [NB: New number!] Date: 06-Oct-04 Time: 23:42:17 ------------------------------ XFMail ------------------------------
On Thu, 7 Oct 2004, Erich Neuwirth wrote:> as.complex("2+1i") -> 2+1i > as.complex("2+i") -> NA > > Does somebody have a modified version of as.complex > which does the coercion in a less strict manner and > produces a complex number also for strings like the > second example? > > Perhaps it would even make sense to change the behavior > of as.complex to handle the second case. > After all, this is an established way of writing > complex numbers in some programming tools > able to deal with complex numbers.R does document that 1i is required, and as.complex uses the same rules for entering complex numbers as e.g. using them as part of an arithmetic expression. Consistency is very important in a programming language. It is easy to change 2+i to 2+1i using sub(), for example. -- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595
Perhaps there could be an extra argument to as.complex that controls whether parsing is standard or loose. Patrick Burns Burns Statistics patrick at burns-stat.com +44 (0)20 8525 0696 http://www.burns-stat.com (home of S Poetry and "A Guide for the Unwilling S User") Prof Brian Ripley wrote:>On Thu, 7 Oct 2004, Erich Neuwirth wrote: > > > >>as.complex("2+1i") -> 2+1i >>as.complex("2+i") -> NA >> >>Does somebody have a modified version of as.complex >>which does the coercion in a less strict manner and >>produces a complex number also for strings like the >>second example? >> >>Perhaps it would even make sense to change the behavior >>of as.complex to handle the second case. >>After all, this is an established way of writing >>complex numbers in some programming tools >>able to deal with complex numbers. >> >> > >R does document that 1i is required, and as.complex uses the same rules >for entering complex numbers as e.g. using them as part of an arithmetic >expression. Consistency is very important in a programming language. > >It is easy to change 2+i to 2+1i using sub(), for example. > > >