[R] using apply

Neha Aggarwal @gg@rw@lneh@2000 @ending from gm@il@com
Wed May 2 21:28:03 CEST 2018


 Hi

I have 3 dataframes, a,b,c with 0/1 values...i have to check a condition
for dataframe a and b and then input the rows ids to datframe c . In the if
condition, I AND the 2 rows of from a and b and then see if the result is
equal to one of them.
I have done this using a for loop, however, it takes a long time to execute
with larger dataset..Can you help me do it using apply function so that i
can do it faster?

a
  V1.x V2.x V3.x V1.y V2.y V3.y
1    1    1    0    1    0    1
2    1    0    1    1    0    1
3    1    1    1    1    0    1
4    0    0    0    1    0    1

b
  V1 V2 V3 V4 V5 V6
1  1  0  1  1  0  0
2  1  0  1  0  0  0

c
             x    y
1          2    1
2          2    2
3          3    1
4          3    2

for(i in 1:nrow(a)){
  for(j in 1:nrow(b)){
    if(all((a[i,]&b[j,])==b[j,]))
    { c[nrow(c)+1, ]<-c(paste(i,j)
      }
  }
}


Thanks,
Neha

	[[alternative HTML version deleted]]




More information about the R-help mailing list