Dear friends, suppose i have two datasets: A and B A: id<-1:6 x<-c(1,2,3,4,5,6) y<-c(2,4,6,8,3,2) xy<-data.frame(id,x,y) B m<-c(1,1,3,3,5,5) n<-c(2,2,6,6,3,3) mn<-data.frame(m,n) Now, i want to perfomr two tasks: 1. get a subset of B,no duplicate values,: C: m n 1 2 3 6 5 3 2.Extract the values in A on the conditions that x=m and y=n the results should be: id x y 1 1 2 3 3 6 5 5 3 Thanks very much! -- Kind Regards, Zhi Jie,Zhang ,PHD Department of Epidemiology School of Public Health Fudan University Tel:86-21-54237149 [[alternative HTML version deleted]]
merge(mn[sample(1:nrow(mn), 3, rep=F),], xy, by.x=c("m","n"), by.y=c("x","y")) ------------------------------------------------------------------- Jacques VESLOT CNRS UMR 8090 I.B.L (2?me ?tage) 1 rue du Professeur Calmette B.P. 245 59019 Lille Cedex Tel : 33 (0)3.20.87.10.44 Fax : 33 (0)3.20.87.10.31 http://www-good.ibl.fr ------------------------------------------------------------------- zhijie zhang a ?crit :> Dear friends, > suppose i have two datasets: A and B > A: > id<-1:6 > x<-c(1,2,3,4,5,6) > y<-c(2,4,6,8,3,2) > xy<-data.frame(id,x,y) > B > m<-c(1,1,3,3,5,5) > n<-c(2,2,6,6,3,3) > mn<-data.frame(m,n) > Now, i want to perfomr two tasks: > 1. get a subset of B,no duplicate values,: > C: > m n > 1 2 > 3 6 > 5 3 > > 2.Extract the values in A on the conditions that x=m and y=n > the results should be: > id x y > 1 1 2 > 3 3 6 > 5 5 3 > Thanks very much! > > > >
mnu <- unique(mn[order(mn$m,mn$n),]) mnu$p <- table(paste(mn$m, mn$n)) merge(mnu[sample(1:nrow(mnu), size=3, prob=mnu$p, replace=F), c("m","n")], xy, by.x=c("m","n"), by.y=c("x","y")) ------------------------------------------------------------------- Jacques VESLOT CNRS UMR 8090 I.B.L (2?me ?tage) 1 rue du Professeur Calmette B.P. 245 59019 Lille Cedex Tel : 33 (0)3.20.87.10.44 Fax : 33 (0)3.20.87.10.31 http://www-good.ibl.fr ------------------------------------------------------------------- zhijie zhang a ?crit :> Dear friends, > suppose i have two datasets: A and B > A: > id<-1:6 > x<-c(1,2,3,4,5,6) > y<-c(2,4,6,8,3,2) > xy<-data.frame(id,x,y) > B > m<-c(1,1,3,3,5,5) > n<-c(2,2,6,6,3,3) > mn<-data.frame(m,n) > Now, i want to perfomr two tasks: > 1. get a subset of B,no duplicate values,: > C: > m n > 1 2 > 3 6 > 5 3 > > 2.Extract the values in A on the conditions that x=m and y=n > the results should be: > id x y > 1 1 2 > 3 3 6 > 5 5 3 > Thanks very much! > > > >