Compare two vector in R

后端 未结 2 1139
没有蜡笔的小新
没有蜡笔的小新 2021-01-18 20:57

I have two vectors:a = c(1,2,3), b = c(1,2,3)

I want to test whether a is exactly the same as b. I know the resul

相关标签:
2条回答
  • 2021-01-18 21:45

    We can use identical

    identical(a,b)
    #[1] TRUE
    

    Or if we there are some difference in attributes which we need to avoid in the comparison, use all.equal

    all.equal(a,b, check.attributes=FALSE)
    #[1] TRUE
    

    Or using similar approach in the OP's post, we can make it compact with all

    all(a==b)
    #[1] TRUE
    

    The number of characters in the above approach is less...

    nchar("identical(a,b)")
    #[1] 14
    nchar("all(a==b)")
    #[1] 9
    
    0 讨论(0)
  • 2021-01-18 21:46

    In addition to the answer above; you could also consider the package 'compare'.

    library(compare)
    compareEqual(a,b)#TRUE
    
    0 讨论(0)
提交回复
热议问题