code error using pipe %>% 'object not found'

I'm just learning programming so I'd appreciate some help with a solution to my problem but also a source to help better understand the underlying issue. I'm sure I have an environment issue, and in second code chunk I've tried to address it, based on Karlijn Willems' article https://www.datacamp.com/tutorial/pipe-r-tutorial, but still get the same error.

I'm also getting an environment error when I try and knit the R markdown document: "Quitting from lines 10-17 (child_stays_markdown.Rmd) Error in eval(expr, envir, enclos) : object 'stays_weekend_in_nights' not found"
Insights on how to get the R markdown document format passed an error in a code chunk moved also appreciated.

Thanks


title: "hotel_bookings_pipe"
output: html_document
date: "2022-11-03"

trim1_df <- bookings_df %>% 
   select(children, stays_in_weekend_nights) %>% 
    subset(children > 0) %>% 
    total <-  sum(stays_weekend_in_nights, na.rm = TRUE)

total
head(trim1_df) 

 ## Error: object 'stays_weekend_in_nights' not found

```{r}
env <- environment()
trim1_df <- bookings_df %>% 
   select(children, stays_in_weekend_nights, envir = env) %>% 
    subset(children > 0) %>% 
total <-  sum(stays_weekend_in_nights, na.rm = TRUE)

total
head(trim1_df)
 
## Error: object 'stays_weekend_in_nights' not found

How did you read in bookings_df? If you view bookings_df do you see that column?

1 Like

When you knit an Rmd document the code gets executed in a clean environment other than the one you are currently working on so the mentioned data frame doesn't exist there and any non-base function has been loaded. You need to include the necessary code to import the data frame into memory and to load packages, in your Rmd document itself, for example, if the data frame comes from a csv file you would include something like this: your_data_frame <- read.csv("path_to_file.csv")and to load the tidyverse you wood need to add library(tidyverse)

thanks got it, I'll give it a try.

bookings_df is loaded into the environment via the hotel_bookings.csv file. I can view a tibble of the columns if I comment out any of the code lines relating to 'stays_weekend_in_nights'

Does your markdown file read in the csv? As @andresrcs explains, markdown runs in it's own space--separate from the regular R space.

It doesn't do it directly in the RM but i have the same issue when I try and execute the code directly in the console. Based on @andresrcs' comment I'll add it to the RM, but i'm pretty sure that's not the issue.
I only put the code in the RM, trying to post the comment here, after I was getting the error running in a Rstuido script.
In any case in RM when I comment out the 'sum' line II get the tibble from the 'head()' showing the column name. Everything seems OK until I try and execute the 'sum' line. I can see the column in the tibble in RM, so I'm sure the column is available, that's the frustrating part. I can see it, but after the %>% call to run the sum is when I get the error. This is why I think I have an environment issue excluding RM.
Be patient , thanks.

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:

I've run the code on the iris data_file and receive the same error.
I use reprex to copy the code .
I hope this helps.
Thanks for the suggestions

data("iris")

library(tidyverse)
library(magrittr)

len_df <- iris %>%
+   select(Sepal.Length, Petal.Length) %>% 
+   subset(Sepal.Length > 5.0) %>% 
+   total <-  sum(Petal.Length, na.rm = TRUE)
#> Error in eval(expr, envir, enclos): object 'Petal.Length' not found

Created on 2022-11-05 with reprex v2.0.2

Very helpful.

Just get rid of the pluses.

I thought they were put in by reprex but here is a freshversion( non +)

data("iris")

library(tidyverse)
library(magrittr)

len_df <- iris %>%
   select(Sepal.Length, Petal.Length) %>% 
   subset(Sepal.Length > 5.0) %>% 
   total <-  sum(Petal.Length, na.rm = TRUE)
total
#> Error in eval(expr, envir, enclos): object 'Petal.Length' not found

Created on 2022-11-05 with reprex v2.0.2

It is not clear what you are trying to do but you are mixing dplyr and base R syntax in a way that is impossible.
Is this what you are trying to achieve?

library(tidyverse)

iris %>%
    select(Sepal.Length, Petal.Length) %>% 
    filter(Sepal.Length > 5.0) %>% 
    summarise(total =  sum(Petal.Length, na.rm = TRUE))
#>   total
#> 1 509.2

Created on 2022-11-05 with reprex v2.0.2

I'm trying to sum the Petal.Length for all Sepal.Length > 5.0 using the %>%

Ok, that's what I did with my code above so, is your problem solved?

It does with the Iris data, but not in my original script.
More studying on dplyr, and I know what to watch out for. Thanks
How did you print the total?

I'm now getting a different error, but I've made so many edits I need to start fresh.
Problem while computing total = sum(stays_weekend_in_nights, na.rm = TRUE).
Caused by error in mask$eval_all_summarise():
! object 'stays_weekend_in_nights' not found

andresrcs,
code works thanks
Although I'm still not getting any output

Sorry but it is not clear what you mean, can you clarify?

I am not getting a total

never mind, I got it.
Thanks again

This topic was automatically closed 42 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.