This seems like really odd behavior in dplyr filtering, could someone tell me where I am erring or if the below occurs on your machine as well. Thank you in advance.
When I try to filter a data frame using dplyr it works fine when using the original character string, but does not (at least on my machine) when pasting in or using a variable at all.
library(dplyr)
###Recreate data
country <- "ZAF"
df <- data.frame(iso3c=as.character("ZAF"),
country=as.character("South Africa"),
level=as.integer(4),stringsAsFactors = FALSE)
Problem is that these filter out the ZAF row, not keep it.
###Filters out the ZAF row, none of these work
df.nowork1 <- filter(df, iso3c==country)
df.nowork2 <- filter(df, iso3c==paste(country))
df.nowork3 <- filter(df, iso3c==paste0("'",country,"'"))
These do as I want them to do with a variable:
##Works
df.works <- filter(df, iso3c=="ZAF")
df.works2 <- filter(df, iso3c==paste("ZAF"))