Learn R Programming

broman (version 0.84)

cf: Compare objects, including missing data pattern

Description

Check whether two objects are the same, including their patterns of NAs.

Usage

cf(a, b)

Value

Boolean object with TRUE indicating an element is the same.

Arguments

a

Some object.

b

Another object

Details

It's not very complicated: ((is.na(a) & is.na(b)) | (!is.na(a) & !is.na(b) & a == b))

Examples

Run this code
x <- c(5, 8, 9, NA, 3, NA)
y <- c(5, 2, 9, 4, NA, NA)
cf(x,y)

x <- matrix(rnorm(1000), ncol=20)
x[sample(seq(along=x), 100)] <- NA
all(cf(x,x))
dim(cf(x,x))

y <- x
y[4,8] <- NA
sum(!cf(x,y))
y[6,2] <- 18
sum(!cf(x,y))
y[6,5] <- 32
sum(!cf(x,y))

x <- as.data.frame(x)
y <- as.data.frame(y)
sum(!cf(x,y))

x <- as.list(x)
y <- as.list(y)
sapply(cf(x,y), function(a) sum(!a))

Run the code above in your browser using DataLab