'Get the same rows from two data frames without merge or dplyr
I have to get the same rows from two datasets without using function as merge or packages like dplyr. basically I can only use for cycles and if.
I've come up to this solution:
#since the two data frames are really big, I've reduced them using:
tab1 <- tab1[seq(800,1000),]
tab2 <- tab2[seq(800,1000),]
rname1 <- rownames(tab1)
rname2 <- rownames(tab2)
vecres <- c()
#since I need the results from only the first 3 columns of datasets:
for (i in rname1) {
a <- tab1[i,c(1,2,3)]
for (j in rname2) {
b <- tab2[j,c(1,2,3)]
cond <- a == b
singlecond <- all(cond)
if (singlecond) {vecres[i] <- c(a[i,c(1,2,3)])}
}
}
I. don't know how to go on and where I'm making mistakes... please help!
Solution 1:[1]
You can try the code below
tab1[do.call(paste, tab1[1:3]) %in% do.call(paste, tab2[1:3]), ]
If you really want for loops, you can try
vecres <- c()
for (i in rname1) {
a <- tab1[i, c(1, 2, 3)]
for (j in rname2) {
b <- tab2[j, c(1, 2, 3)]
cond <- a == b
singlecond <- all(cond)
if (singlecond) {
vecres <- c(vecres, i)
}
}
}
tab1[vecres,]
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
