[R] Dataframe manipulation question

John Fox jfox at mcmaster.ca
Wed Oct 6 22:57:23 CEST 2004


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/




More information about the R-help mailing list