[R] using apply

David L Carlson dc@r|@on @end|ng |rom t@mu@edu
Wed May 2 23:40:56 CEST 2018


This might be faster. It uses apply() which is just another way of constructing a loop so it is not necessarily faster. We can vectorize the logical comparisons, but all() is not vectorized. Use dput() to share your data. That makes it easier to replicate your example:

a <- structure(list(V1. = c(1, 1, 1, 0), V2.x = c(1, 0, 1, 0), V3.x = c(0, 
     1, 1, 0), V1.y = c(1, 1, 1, 1), V2.y = c(0, 0, 0, 0), V3.y = c(1, 1, 
     1, 1)), .Names = c("V1.x", "V2.x", "V3.x", "V1.y", "V2.y", "V3.y"),
     row.names = c(NA, -4L), class = "data.frame")

b <- structure(list(V1 = c(1, 1), V2 = c(0, 0), V3 = c(1, 1), V4 = c(1, 
     0), V5 = c(0, 0), V6 = c(0, 0)), .Names = c("V1", "V2", "V3", 
     "V4", "V5", "V6"), row.names = c(NA, -2L), class = "data.frame")

# Generate the row indices that we need so we can vectorize the logical operation:
idxa <- rep(1:4, each=2)
idxb <- rep(1:2, 4)
ab <- (a[idxa, ] & b[idxb, ]) == b[idxb, ]
c <- cbind(idxa, idxb)[apply(ab, 1, all), ]
c
#      idxa idxb
# [1,]    2    1
# [2,]    2    2
# [3,]    3    1
# [4,]    3    2

----------------------------------------
David L Carlson
Department of Anthropology
Texas A&M University
College Station, TX 77843-4352




-----Original Message-----
From: R-help <r-help-bounces using r-project.org> On Behalf Of Ulrik Stervbo
Sent: Wednesday, May 2, 2018 3:49 PM
To: Neha Aggarwal <aggarwalneha2000 using gmail.com>
Cc: r-help using r-project.org
Subject: Re: [R] using apply

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 using 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 using 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]]

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




More information about the R-help mailing list