Object not found but it is listed in the header

dplyr
ggplot2
rstudio

#1

I have loaded the following libraries:

library(dplyr)
library(ggplot2)
library(tidyr)

#  Import the Ash data into a data table called “Lab_Ash_Data”
Lab_Ash_Data <- Lab_Ash_Weights
str(Lab_Ash_Data)

But the following code produces an error:

# Calculate Ash Variables
# Use a PIPE to filter out the missing values using filter()
# Sequentially calculate “InitialWeight”, “FinalWeight” and “Ash” as new variables using mutate()

Lab_Ash_Calculated <- Lab_Ash_Data %>% filter(InitialSampleTinWeight ==0 &                                                  
                                            FinalSampleTinWeight ==0 &
                                            mutate(Ash_Data=Ash_Data$FinalWeight)/ InitialWeight)      

Error in filter_impl(.data, quo) : 
  Evaluation error: object 'InitialSampleTinWeight' not found.         

Yet the object is a header that was imported with the data. Why am I receiving this error?


#2

hey @ppines - I think there might be a couple of things going on with your pipeline as it stands right now. Off the bat I can see what I think is a missing closing parentheses in your code. I believe you need a ) after the second condition for filter() followed by the pipe operator:

Lab_Ash_Calculated <- Lab_Ash_Data %>% 
    filter(InitialSampleTinWeight ==0 &
        FinalSampleTinWeight ==0) %>%
    mutate(Ash_Data=Ash_Data$FinalWeight)/ InitialWeight)

I'm assuming that 0 values in InitialSampleTinWeight and FinalSampleTinWeight are what you refer to as "missing"? If so, you're actually subsetting all of the missing data into a single object instead of subsetting all of the valid data. Using != will reverse this behavior.

The mutate() call also mixes in some base R "dollar sign" syntax in it that you'll want to clean up. You are currently creating a new variable named Ash_Data and then making reference to a data frame also called Ash_Data. There is also a closing ) in the midst of your mutate() call that will cause an error as well.

I suspect this is closer to what you ultimately want:

Lab_Ash_Calculated <- Lab_Ash_Data %>% 
    filter(InitialSampleTinWeight != 0 &
        FinalSampleTinWeight != 0) %>%
    mutate(Ash_Ratio = FinalWeight/ InitialWeight)

Hope these observations are helpful!


#3

Hi Chris, thanks for the prompt response! I have entered your amended code and it hasn't made a difference..... still getting the same error.


#4

Sorry to hear that @ppines! Are you sure that a column named InitialSampleTinWeight exists in Lab_Ash_Data with that exact spelling?


#5

Good thinking... turns out I did mistype, I have amended the code :slight_smile:

Lab_Ash_Calculated <- Lab_Ash_Data %>% 
  filter(InitialSampleInTinWeight != 0 &
           FinalSampleInTinWeight != 0) %>%
  mutate(Ash_Ratio = FinalWeight/ InitialWeight)

However, it has produce a different error:

Error in mutate_impl(.data, dots) : 
  Evaluation error: object 'FinalWeight' not found.

#6

I know how frustrating debugging pipelines can be @ppines! Does FinalWeight exist in the original data?

The mutate() function requires existing variables on the right hand side of the equals sign. On the left side, you can give either an existing variable or a new variable.

If FinalWeight does exist, check for spelling issues. If it does not exist, you’ll need to create it first in your pipeline before calculating the ratio.