Compare two vector in R

后端 未结 2 1143
没有蜡笔的小新
没有蜡笔的小新 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
    

提交回复
热议问题