Do not consider NA is a function

Hello,

I have two column in a data.frame and I want to use some function for comparaison. For example intersect(). Because the length of the column are not the same, there is NA in there. However, when I use intersect, NA is considered that I do not want.

Example:

head(df)
A B
1 cat dog
2 dog elephant
3 duck
4

intersect(df$A,df$B)
[1] "dog" NA

Hope somebody can help me
Best
Antonin

You could remove the rows where A and B both contain NA values and then run intersect().

df <- data.frame(A = c("cat", "dog", "duck", NA),
                 B = c("dog", "elephant", NA, NA))

df_new <- subset(df, !is.na(A) & !is.na(B))

intersect(df_new$A, df_new$B)
#> [1] "dog"

Created on 2020-05-11 by the reprex package (v0.3.0)

Thank you for taking the time to answer.
Best

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.