Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

r - Extracting common character strings from multiple vectors of different lengths

Is there function in R to find the common characters in multiple vectors (of different lengths). For example, if I have 3 vectors...

 a1 <- LETTERS[1:7]
 a2 <- LETTERS[4:8]
 a3 <- LETTERS[2:10]

a1
# [1] "A" "B" "C" "D" "E" "F" "G"
a2
# [1] "D" "E" "F" "G" "H"
a3
# [1] "B" "C" "D" "E" "F" "G" "H" "I" "J"

I can think of a messy solutions...

intersect(intersect(a1,a2),a3)
# [1] "D" "E" "F" "G"

Problem is, I have around 8 or 9 vectors. Is there a better way to this?

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

Yes:

Reduce(intersect,list(a1,a2,a3))

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...