How do you test if two vectors are identical without using the identical or equa
ID: 3696333 • Letter: H
Question
How do you test if two vectors are identical without using the identical or equal command in R?
Hints:
You should not use a looping construct because we have not yet covered that.
Just replacing NA’s by some other value (say 0) and then looking at x==y won’t quite work, because x[1] could be NA while y[1] could be zero. Remember, your function should work for all x and y.
When trying to solve the issue of NA’s, the comparison x==y does not work because you get NA when it is in either x or y in some (not necessarily same) position. However, comparing is.na(x) and is.na(y) (using ==) lets you find out whether any NA’s are in the same positions, by the TRUE and FALSE patterns. They should be the same if all NA’s are in the same positions. Thus, try to first ascertain whether the NA pattern in x and y are the same, getting a TRUE or FALSE as the result.
After having done that replace all NA’s in x, y by any number, say 0, and test x == y. If the NA in x, y are all in the same place (if they don’t then the previous test for matching NA patterns will give you a FALSE anyway), and if all other components match, then you should get a vector with all TRUE. Sum that and compare it with the length of x. Put these two tests (for matching NA pattern) and the other test together in a logical & statement. You can assign the logic value for the first test to a variable named A and that of the second test to a variable B. If both are TRUE you will get TRUE, otherwise you get FALSE.
Create a function MyIdentical <- function(x,y)
{ your commands here ... }
Explanation / Answer
function body.
MyIdentical <- function (x,y){
A <- length(x)==length(y)
# A is used to check equal length
n <- min(length(x),length(y))
x <- x[1:n]
y <- y[1:n]
# making both vectors of equal length, if they are not,
# otherwise x and y remain unchanged.
B <- sum(is.na(x)==is.na(y))==n
# checking whether NA pattern in both vectors are the same
# i.e., is.na(x)==is.na(y) should result in a TRUE vector of length n.
x[is.na(x)] <- 0
y[is.na(y)] <- 0
# replacing all NA's by 0
# if the rest of the vector components match they will
# still match provided the NA patterns are the same.
# There are no more NA's in x or y by doing this substitution.
C <- sum(x==y)==n
# checks whether all components in x and y match, thus we should
# have n TRUE's in x==y.
D <- A&B&C
# both vectors are identical in length, NA patterns, and other
# components if and only of A = TRUE & B = TRUE & C = TRUE
# Thus output D
D
}
checks on various cases
> MyIdentical(1:7,1:7)
[1] TRUE
> MyIdentical(1:7,1:8)
[1] FALSE
> MyIdentical(1:7,c(NA,2:7))
[1] FALSE
> MyIdentical(c(NA,2:7),c(NA,2:7))
[1] TRUE
> MyIdentical(c(NA,2:7),c(NA,2:6,NA))
[1] FALSE
> MyIdentical(1:7,c(0,2:7))
[1] FALSE
> MyIdentical(letters[1:7],letters[1:7])
[1] TRUE
> MyIdentical(c(letters[1:7],NA),c(letters[1:7],NA))
[1] TRUE
checking identical components, with or without NA's, different lengths, different NA patterns,different numbers without NA's, and also for equal character (and NA) patterns.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.