[R] extract common rows from a dataframe

arun smartpink111 at yahoo.com
Tue Jun 11 22:54:58 CEST 2013


Hi,
Try this:
dat1<- read.table(text="
DEPTH SALINITY DEPTH SALINITY
18    87    39.06    94    39.06
19  173    39.05  141      NA
20  260    39.00  188    39.07
21  312    38.97  207    39.03
22    1    39.36    1    39.35
23    10    39.36    10    39.33
24    20    39.36    20    39.33
25    30    39.35    30    39.33
",sep="",header=TRUE)

library(matrixStats)
res<-dat1[rowDiffs(as.matrix(dat1[,c(1,3)]))==0,]
res
#   DEPTH SALINITY DEPTH.1 SALINITY.1
#22     1    39.36       1      39.35
#23    10    39.36      10      39.33
#24    20    39.36      20      39.33
#25    30    39.35      30      39.33


#or
dat1[apply(dat1,1,function(x) x[1]-x[3]==0),]
#   DEPTH SALINITY DEPTH.1 SALINITY.1
#22     1    39.36       1      39.35
#23    10    39.36      10      39.33
#24    20    39.36      20      39.33
#25    30    39.35      30      39.33
A.K.



----- Original Message -----
From: Ellie Papazisi <elliepapazisi at gmail.com>
To: r-help at r-project.org
Cc: 
Sent: Tuesday, June 11, 2013 11:45 AM
Subject: [R] extract common rows from a dataframe

Hello, I'm trying to extract the common rows from a data frame, which is
something like this :


*   DEPTH SALINITY DEPTH SALINITY*
*18    87    39.06    94    39.06*
*19   173    39.05   141       NA*
*20   260    39.00   188    39.07*
*21   312    38.97   207    39.03*
*22     1    39.36     1    39.35*
*23    10    39.36    10    39.33*
*24    20    39.36    20    39.33*
*25    30    39.35    30    39.33*
*
*
i want to extract the rows with the same depth.
i cannot do something like *data.frame[22:25,]* because i have lots of rows

any help?
thank you

    [[alternative HTML version deleted]]

______________________________________________
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