How do I filter for age < 78 in my df?

This is my first experience with coding and it's been challenging to say the least.

library(tidyverse)
library(dslabs)
lbrary(dplyr)
Library(babynames)

dfnew <- babynames[,c(1:5)] %>%
mutate(age = 2022-year) %>%
filter(dfnew, age < 78)

I keep getting this error: Error in filter():
! Problem while computing ..1 = dfnew.
The code works up to the filter function
:heavy_multiplication_x: Input ..1$year must be a logical vector, not a integer.

Are you sure? If your code here is what you actually have then you have typos.

lbrary(dplyr) should be library(dplyr) & Library(babynames) should be library(babynames)

However the real problem seems that
filter(dfnew, age < 78)
should be
filter(age < 78)
You are creating and filtering the age variable from babynames. You have not created dfnew until the mutate and filtre functions are finished.

This should work:

library(tidyverse)
library(dslabs)
library(dplyr)
library(babynames)

dfnew <- babynames[,c(1:5)] %>%
  mutate(age = 2022-year) %>%
  filter( age < 78)
1 Like

I have edited the code as below.
When you are creating new data frame, there is no need for mentioning the new data frame's name inside filter function.

library(tidyverse)
library(dslabs)
library(babynames)

dfnew <- babynames[,c(1:5)] %>%
  mutate(age = 2022-year) %>%
  filter(age < 78)

Thank you very much.

Thank you. I did typo that by hand since I had cleared my console. I'll try and be more careful.
It does work now. Thanks again.

Typos haunt R. (T just misspelled "typo").

If your question's been answered , would you mind choosing a solution? It helps other people see which questions still need help, or find solutions if they have similar problems. Here’s how to do it:

https://support.rstudio.com/hc/en-us/articles/200534577-Resetting-RStudio-Desktop-s-Statehttps://community.rstudio.com/t/faq-how-do-i-mark-a-solution/5633

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.