Matching up two vectors in R

后端 未结 2 1150
失恋的感觉
失恋的感觉 2020-12-01 18:40

I have two vectors in R as shown below. The first one represents amino acid numbers with some positions missing while the second one represents the full list. I need to some

相关标签:
2条回答
  • 2020-12-01 18:51

    to return the indices use %in%

    > (a <- letters[1:5])
    [1] "a" "b" "c" "d" "e"
    > b <- c("a", "b", "e", "f")
    > (which(a%in%b))
    [1] 1 2 5
    > (which(b%in%a))
    [1] 1 2 3
    
    0 讨论(0)
  • 2020-12-01 19:08

    Use match:

    match(PDBRenum.3V5Q[,2], PDBXPoly.3V5Q[[1]][,3])
    

    With match(x,y), the ith element of the output is the first index of y that matches x[i], unless x[i] doesn't appear in y, in which case it gives NA.

    match is most useful when y has no repeated values.

    For example:

    > match( c(1,3,5), c(2,6,7,3,1) )
    [1]  5  4 NA
    > match( c(1,3,5), c(2,6,7,3,1, 8,1,3) )
    [1]  5  4 NA
    
    0 讨论(0)
提交回复
热议问题