Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
248 views
in Technique[技术] by (71.8m points)

r - How to create a function that prints the number of identical values in three vectors?

please...can you tell me how can I create a function that finds the number of identical values in three vectors? And the result should be 2. (beause 8. and 10. are same in vectors) The vectors are:

a = 1:10
b = 4:15
d = c(8, 10, 18)
question from:https://stackoverflow.com/questions/65887857/how-to-create-a-function-that-prints-the-number-of-identical-values-in-three-vec

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I think the most efficient way is the answer by @Dason. Below is another option with mapply + rowSums

v <- list(a,b,d)
sum(rowSums(mapply(`%in%`,list(unique(unlist(v))),v))==length(v))

which gives

[1] 2

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...