-3
I have an array of all possible values that appear in a set of columns of a data frame. The number of components in the vector is different from the number of observations of the data frame.
My goal is to identify the invalid observations in the data frame. The solution below worked, but I am not satisfied as I would like to not use the for
loop.
DF
is a data frame with 4 columns named da1
to da4
and 9 lines of observations. DF
was imported from an excel spreadsheet as well as "check" which is an excel table with a single column.
str(DF)
DF1 <- DF
DF1 <- as.data.frame(DF) # transformo DF em data frame
str(DF1)
result
is a logic vector of length equal to the number of lines of observations of DF1
.
result1
is a logical data frame obtained from DF
.
result <- logical(length=9)
result1 <- as.data.frame(result)
The loop below checks whether each column of DF1
whether or not it has some element of "check". result1
is the resulting logical data frame.
for (col in 1:4) {
diag <- DF1[, col]
result1[,col] <- is.element(diag,check)
}
result1
result1 <- sapply(DF1, function(x) x %in% check)
.– Rui Barradas