r - counting matching elements of two vectors but not including repeated elements in the count -
i've search lot in forum. however, didn't found similar problem 1 i'm facing.
my question is: have 2 vectors x <- c(1,1,2,2,3,3,3,4,4,4,6,7,8)
, z <- c(1,1,2,4,5,5,5)
i need count number of times x or z appears in each other including if repeated or not.
the reply problem should 4 because : there two number 1, one number 2, , one number 4 in each vector.
functions match() don't help since homecoming reply of repeated non repeated numbers. using unique() alter final reply 4 3
what came loop every time found 1 number in other, remove list won't counted again. loop works fine size of example; however, searching larger vectors numerous times makes loop inefficient , slow purposes.
system.time({ for(n in 1:1000){ x <- c(1,1,2,2,3,3,3,4,4,4,6,7,8) z <- c(1,1,2,4,5,5,5) score <- 0 for(s in spectrum){ if(s %in% sequence){ sequence <- sequence[-which(sequence==s)[1]] score <- score + 1 } } } })
can suggest improve method? i've tried using lapply, short vectors faster, became slower longer ones..
use r's vectorization advantage here. there's no looping necessary.
you utilize table
@ frequencies,
table(z[z %in% x]) # # 1 2 4 # 2 1 1
and take sum
of table total
sum(table(z[z %in% x])) # [1] 4
r performance optimization pattern-matching match
No comments:
Post a Comment