Guillaume Bal
2013-Aug-23 21:37 UTC
[R] how to combine apply and which or alternative ways to do so
Dear useRs, I am currently doing some data cleaning and data manipulation and I have the following problem. I have two vectors. Let say the size of the first one is 10 000 (vector 1) and the size of the second one is 1 000 000 (vector 2). I need to know for each cell of vector 1 which cells of vector 2 correspond. I tried to combine the "which" and "apply" but I dit not succeed and I have no idea about a possible vectorization. Can someone help me ? Thanks Guillaume
arun
2013-Aug-24 06:14 UTC
[R] how to combine apply and which or alternative ways to do so
HI, May be this helps: set.seed(24) ?vec1<- sample(1e5,1e4,replace=FALSE) ?set.seed(48) ?vec2<- sample(1e7,1e6,replace=FALSE) ?system.time(res<- sapply(vec1,function(x) which(vec2%in%x))) #?? user? system elapsed #212.520?? 0.500 213.386 #or system.time({res2<- match(vec1,vec2) #assuming that there are no duplicate elements ??? ??? ?res3<- res2[!is.na(res2)]}) # user? system elapsed #? 0.044?? 0.000?? 0.044 identical(unlist(res),res3) #[1] TRUE A.K. ----- Original Message ----- From: Guillaume Bal <balguillaume2883 at gmail.com> To: r-help at r-project.org Cc: Sent: Friday, August 23, 2013 5:37 PM Subject: [R] how to combine apply and which or alternative ways to do so Dear useRs, I am currently doing some data cleaning and data manipulation and I have the following problem. I have two vectors. Let say the size of the first one is 10 000 (vector 1) and the size of the second one is 1 000 000 (vector 2). I need to know for each cell of vector 1 which cells of vector 2 correspond. I tried to combine the "which" and "apply"? but I dit not succeed and? I have no idea about a possible vectorization. Can someone help me ? Thanks Guillaume ______________________________________________ R-help at r-project.org mailing list 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.
John Kane
2013-Aug-24 12:41 UTC
[R] how to combine apply and which or alternative ways to do so
Bon jour, I am not clear exactly what you are looking for. When I first read the email I though you wanted to know if something in vector 2 was in vector 1 but after rereading it I am not sure. If I was correct the first time I think something like vector2 %in% vector1 will do it. If I am wrong, and I think I am, would you explain the problem a bit more, perhaps with some small data vectors? John Kane Kingston ON Canada> -----Original Message----- > From: balguillaume2883 at gmail.com > Sent: Fri, 23 Aug 2013 14:37:26 -0700 > To: r-help at r-project.org > Subject: [R] how to combine apply and which or alternative ways to do so > > Dear useRs, > > I am currently doing some data cleaning and data manipulation and I have > the > following problem. > I have two vectors. Let say the size of the first one is 10 000 (vector > 1) > and the size of the second one is 1 000 000 (vector 2). > > I need to know for each cell of vector 1 which cells of vector 2 > correspond. > > I tried to combine the "which" and "apply" but I dit not succeed and I > have no idea about a possible vectorization. > > Can someone help me ? > > Thanks > > Guillaume > > ______________________________________________ > R-help at r-project.org mailing list > 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.____________________________________________________________ FREE ONLINE PHOTOSHARING - Share your photos online with your friends and family! Visit http://www.inbox.com/photosharing to find out more!