[R] reduce and intersect question (maybe)?
Jeff Newmiller
jdnewm|| @end|ng |rom dcn@d@v|@@c@@u@
Thu Feb 21 14:47:48 CET 2019
Use ?merge instead of intersect.
On February 21, 2019 5:22:46 AM PST, Andras Farkas via R-help <r-help using r-project.org> wrote:
>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
>
>______________________________________________
>R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
>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.
--
Sent from my phone. Please excuse my brevity.
More information about the R-help
mailing list