1

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"))
JD Long
  • 59,675
  • 58
  • 202
  • 294
Neal Barsch
  • 2,810
  • 2
  • 13
  • 39
  • Please do not edit your question with your answer. If you found the solution yourself, you are welcome to answer below; otherwise you can chose another members answer as the one that helped you by clicking the checkmark to the left of the answer. Questions that have been edited to include a solution may be rolled back to the previous state. – K.Dᴀᴠɪs Mar 08 '18 at 06:01

2 Answers2

3

We can do this by specifying not to evaluate with the columns of dataset with the bang bang (!!)

df %>%
   filter(iso3c== !!country)
#  iso3c      country level
#1   ZAF South Africa     4

Or another way is to extract the object from .GlobalEnv

filter(df, iso3c== .GlobalEnv$country)
#  iso3c      country level
#1   ZAF South Africa     4

Here, the 'df' have a column named 'country', so within the enviornment, while specifying the 'country', it is checking the column and not the object outside the dataset

df %>%
    select(country)
#        country
#1 South Africa
akrun
  • 874,273
  • 37
  • 540
  • 662
1

I'm feeling sheepish on keeping a colname & variable the same name.

ctry <- country
works <- filter(df, iso3c==ctry)
Neal Barsch
  • 2,810
  • 2
  • 13
  • 39
  • 1
    I thought you intentionally kept it and wanted to find out how those situations can be evaluated. Anyway, the `!!` would here too – akrun Mar 08 '18 at 06:07
  • 1
    Yeah I like the globals method, so I learned something in the process :) – Neal Barsch Mar 08 '18 at 06:08