'How to filter for variables in a column of one df from another df's column with unequal length in R?

I am trying to select for variables in a column of a DF using the variables from a column in another DF with different length. I am using Dplyer to filter.

DF1                                     
|States | Frequency  |     
|:----  |:------:    |
| AL    | 19         |
| TX    | 15         |
| MO    | 2          |
|CA     | 22         |
|NY     | 66         |

DF2                                     
|States | Total  |     
|:----  |:------:|
| AL    | 11     |
| TX    | 12     |
| MO    | 89     |

I am using the following code to filter

filter(df1,States %in% df2$States)

But I am getting this result

[1] States          frequency
<0 rows> (or 0-length row.names)

How can I get only the variables I am interested in DF1?

Thanks.



Solution 1:[1]

It is an issue of white spaces Remove them with trimws:

library(dplyr)

filter(df1, trimws(States) %in% df2$States)

 States Frequency
1     AL        19
2     TX        15
3     MO         2

Solution 2:[2]

Maybe this is want you want?If so, then it's simply a typo issue.

filter(df1,States %in% df2$States)
  States Frequency
1     AL        19
2     TX        15
3     MO         2

Edit

Here is the data I used to generate the result above:

df1 <- structure(list(States = c("AL", "TX", "MO", "CA", "NY"), Frequency = c(19, 
15, 2, 22, 66)), class = "data.frame", row.names = c(NA, -5L))

df2 <- structure(list(States = c("AL", "TX", "MO"), Frequency = c(11, 
12, 89)), class = "data.frame", row.names = c(NA, -3L))

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 TarJae
Solution 2