I must be missing something

I seem to be getting a bizarre error message, although I'm sure it's something obvious I've done. Help?

rm(list = ls())
library(tibble)

eGrades <- tibble(email = c("Mickey@umail.ucsb.edu", "Minnie@ucsb.edu"))

gradeScope <- tibble(email = c("Mickey@ucsb.edu", "Minnie@ucsb.edu", "Donald@ucsb.edu"),
                     howManyTurnedIn = c(5, 5, 1))

admit <- gradeScope |> filter(howManyTurnedIn >= 5)
#> Error in eval(expr, envir, enclos): object 'howManyTurnedIn' not found

Created on 2023-10-14 with reprex v2.0.2

I think you may just be missing loading dplyr for the filter() function, try my code below

base::library(package = dplyr) # LOOK HERE
base::library(package = tibble)

eGrades <- tibble::tibble(
  email = c("Mickey@umail.ucsb.edu", "Minnie@ucsb.edu")
)

gradeScope <- tibble::tibble(
  email           = c("Mickey@ucsb.edu", "Minnie@ucsb.edu", "Donald@ucsb.edu"),
  howManyTurnedIn = c(5, 5, 1)
)

admit <- gradeScope |> dplyr::filter(howManyTurnedIn >= 5)
print(admit)
1 Like

That makes sense, except that tidyverse loads dplyr.

1 Like

That's pretty much the only thing I changed, so I'm not sure what else it would be. My code seems to be working in my machine so I'm not sure what else what guidance I can provide.

1 Like

Your fix does work. And thank you. But I'd like to understand why library(tidyverse) isn't sufficient.

So usually when you load tidyverse its usually masks the stats::filter() function in favor of dplyr::filter(). For some reason it must not be performing that masking and giving you dplyr by default.

In my code, I only ever explicitly load tibble, not the entire tidyverse, as you did in your original code. Maybe the behavior would be different if you loaded the entire tidyverse.

1 Like

Also if this solution worked for you please mark it as solution for future users.

I hit the heart button. I haven't hit the Solution button because I still don't understand why loading tidyverse...which says it loads dplyr...isn't enough.

So here's the best explanation I can offer:

When tidyverse (or dplyr) loads with base::library(package = tidyverse) # or dplyr

You get a message like the following:

---Conflicts------
dplyr::filter() masks stats::filter()

or

the following objects are masked from 'package:stats':
filter, lag

As you've pointed out - this is the expected tidyverse behavior to prevent the stats::filter() from interfering with dplyr.

However, when I just load tibble (as you did in your original sample) with base::library(package = tibble)

No messages are given and if I do ?filter it pulls up the stats::filter() function. That's all because I loaded tibble instead of tidyverse. Put simply, loading tibble like you did in your example is not loading the entire tidyverse, it's just loading tibble.

1 Like

TRUE but

library(tibble)

FALSE. So, as @qquagliano notes, the only filter() in namespace is from [stats}. If further evidence is needed, run sessionInfo(),

Knew it was obvious...just needed a little help!!!

No problem! It's an odd trait of some tidyverse packages that they load parts of other tidyverse packages without loading the whole thing so easy mistake to make.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.