[R] Select rows based on condition

Rui Barradas rui1174 at sapo.pt
Tue Apr 17 01:39:12 CEST 2012


Hello,

Try

df1 <- read.table(text="
PtName  Visit  Date               Obs          ObsID
S2             1      6/8/2005       KB               3
S2             1      6/8/2005       JB                 5
S3             1     6/8/2005        KB                3
S3             1    6/8/2005         JB                 5      
D1            1     6/12/2007     CD               11
D1            1    6/12/2007       TE                2
D1            1    6/12/2007       MB              4
", header=TRUE)
head(df1)

df2 <- with(df1, sapply(split(df1, list(PtName, Date)),
		                     function(x) if(nrow(x)) x[which(x$ObsID ==
min(x$ObsID)), ]))
df2 <- do.call(rbind, df2)
rownames(df2) <- 1:nrow(df2)
df2

Hope this helps,

Rui Barradas


--
View this message in context: http://r.789695.n4.nabble.com/Select-rows-based-on-condition-tp4562919p4562991.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list