Reputation: 383
Can I know how to check if there's invalid codes in dataframe in R?
I would like to check is if the variables for Code1 to Code5 is matching with my pre-defined codeframe/list.
All the valid values in my pre-defined codeframe/list:
1,3,4,5,6,7,8,9,11
Below is my data frame
Key Code1 Code2 Code3 Code4 Code5
1 1 8 15 NA 11
2 5 3 NA NA NA
3 4 5 11 NA NA
4 11 2 NA NA NA
and would like to have result as below
Key Code1 Code2 Code3 Code4 Code5 Result
1 1 8 15 NA 11 FALSE <-due to invalid code "15"
2 5 3 NA NA NA TRUE
3 4 5 11 NA NA TRUE
4 11 2 NA NA NA FALSE <-due to invalid code "2"
Can anyone help with it?
Thanks a lot.
Upvotes: 1
Views: 970
Reputation: 887213
We can loop through the rows, then use %in%
to check whether all the non-NA elements in that row is found in the vector ('v1')
df1$Result <- apply(df1[-1], 1, FUN = function(x) all(x[!is.na(x)] %in% v1))
df1$Result
#[1] FALSE TRUE TRUE FALSE
v1 <- c(1,3,4,5,6,7,8,9,11)
Upvotes: 1