Hi,
You could use:
which(a%in%b)
#[1] 1 2 3 4 5
a1<-c(1,2,5)
b1<-c(1,3,4,5,7)
which(a1%in%b1)
#[1] 1 3
A.K.
Hello! I created the following function for calculating which elements
in vector a are existant in vector b. However, all I get is NA NA NA and I can?t figure out why. =/
fun <- function(a, b) {
? y <- rep(NA, length(a))
? x <- 0
? while (x <= length(b)) {
? ? x <- x + 1
? ? for (i in 1:length(a)) {
? ? ? ? y[i]<- (a[i] == b[x])
? ? }
? }
? return(y)
}
fun(a<-1:5,b<-1:9)
Thanks for any help!
Function for finding matching values in two vectors.
2 messages · arun, C W
How about ?intersect
a<-1:5;b<-1:9 a
[1] 1 2 3 4 5
b
[1] 1 2 3 4 5 6 7 8 9
intersect(a, b)
[1] 1 2 3 4 5 I haven't used this in simulation, so I don't know how fast it is. -Mike
On Tue, May 28, 2013 at 2:05 PM, arun <smartpink111 at yahoo.com> wrote:
Hi,
You could use:
which(a%in%b)
#[1] 1 2 3 4 5
a1<-c(1,2,5)
b1<-c(1,3,4,5,7)
which(a1%in%b1)
#[1] 1 3
A.K.
Hello! I created the following function for calculating which elements
in vector a are existant in vector b. However, all I get is NA NA NA and I can?t figure out why. =/
fun <- function(a, b) {
y <- rep(NA, length(a))
x <- 0
while (x <= length(b)) {
x <- x + 1
for (i in 1:length(a)) {
y[i]<- (a[i] == b[x])
}
}
return(y)
}
fun(a<-1:5,b<-1:9)
Thanks for any help!
______________________________________________ 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.