0
votes

I have data sets A and B with identical structure. I want to filter set B against a set of levels within one factor where the levels are the same in both A and B. In other words, to only keep rows in B using the common levels for one factor in A and B.

I have used the following to extract the common levels

InBoth <- intersect(levels(A$FactorName , B$FactorName)

Which I believe is a vector of chr and DOES give the correct list of common values.

I just cannot seem to apply this list to filter set B using InBoth such that B subsequently contains only rows where B$Factorname is in the InBoth set.

Am relatively new to R and am learning as I go along - so many thanks in advance if you can help.

UPDATE As soon as I submitted..I thought of something else. The following works to subset B using a (single) known common level

BFiltered <- B[(B$FactorName == "xxx",]

But I want BFiltered to include all the levels in the InBoth item (which appear in the values part of the environment). That is, how to I substitute "xxx" with InBoth?

1

1 Answers

0
votes

This could work.

df_B[df_B$V1 %in% df_A$V1, ]

This will filter df_B, keeping only observation where it has same levels of variable V1 in df_A