indexing - Finding rows which match between 2 dataframes, and the index of them in R -
i have 2 dataframes. first (and know how this) want find rows (the whole row) match between 2 dataframes. can create column in tells me if entire row in b. however, part don't know how find indices in b be.
tl dr; need create new column in a, tells me false if whole row isn't in b, or instead give me index of row in b.
a = as.data.frame(matrix(data = 1:10,nrow=5)) b = as.data.frame(matrix(data = c(1:5,10,7,6,9,8), nrow=5)) set.seed(02138) b = b[sample(nrow(b)),] rownames(b) = 1:nrow(b) a_ = do.call("paste", a[,1:2]) b_ = do.call("paste", b[,1:2]) # gets me true/false of whether there complete row-wise match a$inb = a_ %in% b_ # gets me rows in b b[b_ %in% a_,] # need combining # expected output > a$inb = temp > v1 v2 inb 1 1 6 false 2 2 7 3 3 3 8 false 4 4 9 5 5 5 10 false
you can add this:
a$inb[a$inb] <- as.character(which(b_ %in% a_)) # v1 v2 inb #1 1 6 false #2 2 7 3 #3 3 8 false #4 4 9 5 #5 5 10 false
Comments
Post a Comment