[R] using apply

Ulrik Stervbo ulrik@@tervbo @ending from gm@il@com
Wed May 2 22:49:18 CEST 2018


Hi Neha,

Perhaps merge() from base or join from dplyr is what you are looking for.
data. table could also be interesting.

Hth
Ulrik

On Wed, 2 May 2018, 21:28 Neha Aggarwal, <aggarwalneha2000 at gmail.com> wrote:

>  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]]
>
> ______________________________________________
> R-help at 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.
>

	[[alternative HTML version deleted]]




More information about the R-help mailing list