object 'variable' not found

Hi,

I keep getting, "object 'sexe' not found", when trying to call the variable 'sexe' from my dataset.

names_fr <- readr::read_delim(my_url05, "\t") %>% filter(my_url05, sexe == 2, !is.na(preusuel), annais != "XXXX", preusuel !="_PRENOMS_RARES") %>% complete(preusuel, annais) %>% mutate(nombre = ifelse(is.na(nombre), 0, nombre), sexe = ifelse(is.na(sexe), 2, sexe)) %>% select(-sexe)

Im still quite new to R, so the problem might lay in the way data are loading.
Im asked to load data from an url:

my_url05 <- "https://perso.telecom-paristech.fr/eagan/class/igr204/data/nat1900- 2017.tsv"
names_fr <- readr::read_delim(my_url05, "\t")

An object called "my_url05" are created, holding the url.
Also a spec_tbl_df are created:
`` names_fr`

A tibble: 1 x 1

https://perso.telecom-paristech.fr/eagan/class/igr204/data/nat1900-

1 2017.tsv `

When i load the data "manually", by downloading the dataset myself an placing it in an object, it does contain the variable called "sexe".

R is the latest version and packages are updated.

What else might be the problem?

Thank you

It seems the read_delim() function is working and I see a problem with the next step.

readr::read_delim(my_url05, "\t") %>% 
    filter(my_url05, sexe == 2, !is.na(preusuel), annais != "XXXX", preusuel !="_PRENOMS_RARES")

The %>% operator takes the object on its left side and passes it as the first argument to the function on its right side. The code above the same as

filter(readr::read_delim(my_url05, "\t"), my_url05, sexe == 2, !is.na(preusuel), annais != "XXXX", preusuel !="_PRENOMS_RARES")

What you want is

readr::read_delim(my_url05, "\t") %>% filter(sexe == 2, !is.na(preusuel), annais != "XXXX", preusuel !="_PRENOMS_RARES")

so that the data frame returned by read_delim becomes the first argument of the filter function.

1 Like

Thank you. Same solution was suggested in Stack Overflow. It didn't result in any changes. I fear that the cause is found in some blop in a package or other because the code I've copied was made by the professor. I guess those can be wrong too. :slight_smile:

The URL you are showing doesn't seem to be a valid one, it returns "404 Not Found" error, so, to help us help you, could you please prepare a reproducible example (reprex) illustrating your issue? Please have a look at this guide, to see how to create one:

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