[R] reduce and intersect question (maybe)?

Andras Farkas motyoc@k@ @end|ng |rom y@hoo@com
Thu Feb 21 14:22:46 CET 2019


Hello All,

wonder if you have a suggestion for the following:

we have
a<-data.frame(ID=c(1,2,3,4,5,6,7),date=as.POSIXct(seq(as.Date('2011-01-01'),as.Date('2011-01-07'),by = 1),format='%m/%d/%Y %H:%M'),z=rnorm(7,1,1))
b<-data.frame(ID=c(1,2,3,11,12,13,14,15),date=as.POSIXct(seq(as.Date('2011-01-01'),as.Date('2011-01-08'),by = 1),format='%m/%d/%Y %H:%M'),z=rnorm(8,1,1))
c<-data.frame(ID=c(1,2,3,4,5,6,7,8,9,10),date=as.POSIXct(c(seq(as.Date('2011-01-01'),as.Date('2011-01-05'),by = 1),seq(as.Date('2011-01-11'),as.Date('2011-01-15'),by = 1)),format='%m/%d/%Y %H:%M'),z=rnorm(10,1,1))
d<-data.frame(ID=c(1,2,3,21,22,23,24,25,26,27,28),date=as.POSIXct(c(as.Date('2011-01-01'),as.Date('2011-11-01'),as.Date('2011-01-03'),seq(as.Date('2011-01-01'),as.Date('2011-01-08'),by = 1)),format='%m/%d/%Y %H:%M'),z=rnorm(11,1,1))


#this function will do the obvious and give the IDs that are in all of the data frames based on the ID column

intersect_all <- function(a,b,...){
  Reduce(intersect, list(a,b,...))
}

intersect_all(a$ID,b$ID,c$ID,d$ID)


#I would like to extend this (or use another function) where the function would give all the rows (ie based on both columns as a condition) that are in all of the data frames, so the result should be as below as these 2 rows are in all of the data frames (the fact that the rows that are common in all data frames ie 1 and 3 in my example are I only set up for the sake of convenience, in reality their row number in each of the data frames may be different) . The value of z is of no particular importance, but once the common rows are identified I would want to subset the data frames to get these results:

a[c(1,3),]
b[c(1,3),]
c[c(1,3),]
d[c(1,3),]

much appreciate your input,

thanks

Andras 



More information about the R-help mailing list