0

dplyr count and filter functions seem to be evaluating this differently.

I am trying to write a function that takes in 2 column names and one value for each of those columns. df1 has 12 columns and has 3 possible values for each column.

object_name_list<-c("A", "B") ## A and B are column names for df1 
values_list<-("C', "D")  # C and D are possible values in the two columns 

tib2<-df1 %>% 
 count(!!!syms(object_name_list)) %>% 
  group_by(!!!syms(object_name_list[1])) %>% 
mutate(prop = prop.table(n))

This function works perfectly and outputs the probability table that I want

df2<-df1 %>% 
  filter(!!!sym(object_name_list[1])==values_list[1] & 
    !!!sym(object_name_list[2])==values_list[2])

This line of code leaves df2 blank. If I put A or B explicitly in place of !!!sym(object_name_list[1]) this works just fine.

What has confused me even further is when !!!syms(object_name_list) is run by itself it outputs the error: "invalid argument type". When syms(object_name_list[1]) is run by itself it has no issue. But putting syms(object_name_list[1]) into the first function without !!! causes it to break.

At a total loss to why this is happening.

akrun
  • 874,273
  • 37
  • 540
  • 662
  • In your first set of code, it is `syms` and later it is changed to `sym`. If you use `sym`, then use it with `!!` instead of `!!!` – akrun Apr 04 '23 at 17:57
  • 2
    It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. – MrFlick Apr 04 '23 at 18:04
  • 1
    Please use `dput` to show example data – akrun Apr 04 '23 at 18:14

0 Answers0