Compare two vector in R -
i have 2 vectors:a = c(1,2,3)
, b = c(1,2,3)
i want test whether a
same b
. know result can given sum(a == b) == length(a)
, there elegant way?
we can use identical
identical(a,b) #[1] true
or if there difference in attributes need avoid in comparison, use all.equal
all.equal(a,b, check.attributes=false) #[1] true
or using similar approach in op's post, can make compact all
all(a==b) #[1] true
the number of characters in above approach less...
nchar("identical(a,b)") #[1] 14 nchar("all(a==b)") #[1] 9