I'd like to know if there's an easy way to find an intersection of two named vectors and output the result to a third, named vector.
In my case, I have the following two named vectors:
> test1
ATMG00010 ATMG00030 ATMG00040 ATMG00050 ATMG00060 ATMG00070
462 324 948 396 542 573
> test2
ATMG00010 ATMG00040 ATMG00070
0 0 0
What is the elegant way to find the intersection of the two vectors based on names and then output the name, value pair of test1 into a new named vector, test3?
i.e.,
> test3
ATMG00010 ATMG00040 ATMG00070
462 948 573
In order for me to get on with my life, I found an intermediate solution that got the job done, but the result was not a named vector, just a numerical vector of values from matched names in test1.
matched <- which(names(test1) %in% names(test2))
test3 <- rep(NA, length(test2))
for (i in 1:length(matched)) { test3[i] <- c(test1[[matched[i]]]) }
This gives me the following result:
test3
[1] 462 948 573
As I only know a little bit of python and pretty much nothing about R, the above code was learned through an all day immersion process. Is there a more straightforward way to do this ?
Thank you!