[R] how to combine apply and which or alternative ways to do so
arun
smartpink111 at yahoo.com
Sat Aug 24 08:14:13 CEST 2013
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.
More information about the R-help
mailing list