[R] matched pairs

William Dunlap wdunlap at tibco.com
Fri Jul 22 21:00:02 CEST 2011


Does the following do what you want?  The singletons()
function identifies the entries that appear only once
in a vector and we use its output to eliminate the singleton
entries.

  > singletons <- function(x) !(duplicated(x) | duplicated(x,fromLast=TRUE))
  > d <- data.frame(x=c(101, 102, 101, 105, 102, 101), y=1001:1006)
  > d
      x    y
  1 101 1001
  2 102 1002
  3 101 1003
  4 105 1004
  5 102 1005
  6 101 1006
  > d[!singletons(d$x), , drop=FALSE]
      x    y
  1 101 1001
  2 102 1002
  3 101 1003
  5 102 1005
  6 101 1006

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com 

> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Ellen S.
> Sent: Friday, July 22, 2011 10:02 AM
> To: r-help at r-project.org
> Subject: [R] matched pairs
> 
> Hi all,
> 
> I am hoping to keep certain rows of my data set. These are rows whose value
> in column X is equal to the value in column X for another row. For example:
> 
> 1  1
> 2  1
> 3  2
> 4  3
> 5  4
> 6  4
> 
> >From this I would want the following:
> 
> 1  1
> 2  1
> 5  4
> 6  4
> 
> I am struggling with the for loop. Here is what I have currently:
> 
> ## where to store values
>   pairs.list <- data.frame(NA, nrow(data), ncol(data))
> 
>   ## iterating through data
>   for (i in 1:nn){
> 
>     # check value with next value, store both
>     if(data$X[i]==data$X[i+1]){
>       pairs.list[i] <- data[i]
>       pairs.list[1+1] <- data[i+1]
>     }
> 
>     # if values are different, discard the first, keep the second
>     else{
>       data <- data[which(!data[i]),]
>     }
>   }
> }
> 
> 
> Any suggestions welcome. Thank you!
> 
> E
> 
> --
> View this message in context: http://r.789695.n4.nabble.com/matched-pairs-tp3687257p3687257.html
> Sent from the R help mailing list archive at Nabble.com.
> 
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list