[R] matched pairs

Ellen S. naturallyln at gmail.com
Fri Jul 22 19:02:19 CEST 2011


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.



More information about the R-help mailing list