r - Matching IDs in two datasets -


i have 2 sets of data, comprising pre , post data. respondents have unique ids, , want create subset includes responded both surveys. example dataset:

pre.data <- data.frame(id = c(1:10), y = sample(c("yes", "no"), 10, replace = true),   survey = 1)  post.data <- data.frame(id = c(1:3,6:10), y = sample(c("yes", "no"), 8, replace = true),   survey = 2)  all.data <- rbind(pre.data, post.data) 

i have following function:

match <- function(dat1, dat2, dat3){  #dat1 whole dataset(both stitched together)    #dat2 pre dataset #dat3 post dataset   selectedrows <- (dat1$id %in% dat2$id &                       dat1$id %in% dat3$id)    matchdata <- dat1[selectedrows,]   return(matchdata) }  prepost.match.data <- match(all.data, pre.data, post.data) 

i think there must better way function of doing same thing, cannot think how. how have done seems bit messy. mean, works - want to, can't thinking there's better way.

my apologies if has been asked in similar way unable find - in case please point me towards relevant answer.

note : arun posted same answer in comment bit earlier me.

you can use intersect :

all.data[all.data$id %in% intersect(pre.data$id, post.data$id),] 

which gives :

   id   y survey 1   1 yes      1 2   2  no      1 3   3  no      1 6   6 yes      1 7   7 yes      1 8   8 yes      1 9   9  no      1 10 10 yes      1 11  1  no      2 12  2 yes      2 13  3  no      2 14  6  no      2 15  7 yes      2 16  8 yes      2 17  9  no      2 18 10 yes      2 

Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -