Mutation error - object not found

Although the object exists, R cannot find it. Here's the code:

# Setting up the RStudio environment
#~~~~~~~~~~~
# Libraries
#~~~~~~~~~~
library(dplyr)
library(ggplot2)
library(tidyr)

#~~~~~~~~~~~
# Functions
#~~~~~~~~~~

rescale_01 <-function(x) (x-min(x))/(max(x)-min(x)) -1/2
z_stand<-function(x) (x-mean(x))/sd(x)
ExpectedBrix <- function(x) (x*0.21084778699754 + 4.28455310831511)

#~~~~~~~~~~~~
# Thresholds
#~~~~~~~~~~~
Thresh.Brix.min <- 15
Thresh.Brix.max <- 30

Thresh.Pol.min <- 50
Thresh.Pol.max <- 105
ExpectedBrix.delta <- 1

Thresh.Fibre.min <- 4
Thresh.Fibre.max <- 25
Thresh.Fibre.delta <- .25

Thresh.Ash.min <- 0
Thresh.Ash.max <- 8


# Import the Fibre data into a data table called “Lab_Fibre_Data”

Lab_Fibre_Data <- read.table("Lab_Fibre_Weights.csv", header=TRUE, sep=",", dec =".")


# Calculate Percentage Fibre Variables using direct assignment
# Name the resulting variable “Fibre1” and add this new variable to the “Lab_Fibre_Data” data table, using direct assignment
# Fibre% = 100 * (InitialSampleCanWeight – FinalSampleCanWeight) / SampleWeight

Lab_Fibre_Data$Fibre1 <- 100 * (Lab_Fibre_Data$InitialSampleCanWeight_1 - Lab_Fibre_Data$FinalSampleCanWeight_1) / Lab_Fibre_Data$SampleWeight_1


# Calculate Percentage Fibre Variables using the mutate() function from the dplyr package, call it “Fibre2”
# Add “Fibre2” as a new variable to the “Lab_Fibre_Data” data table

Lab_Fibre_Data <- Lab_Fibre_Data %>% mutate(Fibre2 = 100 *(InitialSampleCanWeight_2 - FinalSampleCanWeight_2) / SampleWeight_2)


# Use the filter() function to remove samples (rows) that contain a missing value

Lab_Fibre_Filtered <- Lab_Fibre_Data %>% filter(InitialSampleCanWeight_1 >0 & 
                                                InitialSampleCanWeight_2 >0 &
                                                FinalSampleCanWeight_1 >0 &
                                                FinalSampleCanWeight_2 >0 &
                                                SampleWeight_1 >0 &
                                                SampleWeight_2 >0)                                              


# UPDATE code in table 5 to include maximum fibre difference limit as an additional filtering criteria
# Absolute difference between the two estimates (computed variables “Fibre1”  and “Fibre2”) = or > 0.25 units
# UPDATE your code in table 5 to include this maximum fibre difference limit as an additional filtering criteria

Lab_Fibre_Filtered <- Lab_Fibre_Filtered %>% filter(!(abs(Fibre1 - Fibre2) >= 0.25))


# Calculate the final fibre estimates by averaging the replicate fibre measurements, “Fibre1” and “Fibre2”
# Use mutate() to calculate the average fibre and add it as a new variable named “Fibre” to the Lab_Fibre_Filtered data table

Lab_Fibre_Filtered <- Lab_Fibre_Filtered %>% mutate(Fibre = (Fibre1 + Fibre2)/2)


# Use a PIPE to sequentially filter the measurements in Lab_Fibre_Filtered to remove the out-of-range fibre values
# Keeping only the rows of the data table for which “Fibre” is greater than Thresh.Fibre.min
# Keeping only the resulting rows for which “Fibre” is less than Thresh.Fibre.max.  Save the resulting data into the Lab_Fibre_Filtered table

Lab_Fibre_Filtered <- Lab_Fibre_Filtered %>% filter(Fibre > Thresh.Fibre.min & Fibre < Thresh.Fibre.max)


# Use select() to save the LabID and Fibre variables (1st and last columns) from Lab_Fibre_Filtered to a new data table called Lab_Fibre

Lab_Fibre <- Lab_Fibre_Filtered %>% select(LabID,Fibre)


#  Import the Ash data into a data table called “Lab_Ash_Data”

Lab_Ash_Data <- read.table("Lab_Ash_Weights.csv", header=TRUE, sep=",", dec =".")


# 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()
# TinWeight (column 2), InitialSampleInTinWeight (column 3), FinalSampleInTinWeight (column 4)
# Ash = 100*FinalWeight/InitialWeight
# InitialWeight=InitialSampleInTinWeight-TinWeight, FinalWeight=FinalSampleInTinWeight-TinWeight 

Lab_Ash_Calculated$Ash <- Lab_Ash_Data %>% 
                          filter(InitialSampleInTinWeight ==0 &                                                  
                                 FinalSampleInTinWeight ==0) &
                          mutate(InitialWeight = InitialSampleInTinWeight) &
                          mutate(FinalWeight = FinalSampleInTinWeight) &
                          mutate(Ash = 100 *(FinalWeight/InitialWeight)) 

Error in mutate(InitialWeight = InitialSampleInTinWeight) : 
  object 'InitialSampleInTinWeight' not found

# Filtering Ash Variables
# Threshold values for ash are: Thresh.Ash.min and Thresh.Ash.max.
# Update pipe to filter out any out-of-range Ash values. Add the additional filter at the end of the pipe.

Lab_Ash_Calculated <- Lab_Ash_Calculated %>% filter(Ash > Thresh.Ash.min & Ash < Thresh.Ash.max)
Error in eval(lhs, parent, parent) : 
  object 'Lab_Ash_Calculated' not found

# Summarising Ash Variables
# Use a PIPE with the grouped_by() and summarize() functions to produce a data table called Lab_Ash 
# Grouped by LabID and summarises variable Ash by taking its grouped mean values
# Resulting table, Lab_Ash, must then have two variables, LabID and Ash, where LabID now contains unique values (no replicates)

Lab_Ash<-Lab_Ash_Calculated %>% grouped_by(LabID) %>% summarize(Ash=mean(Ash))
Error in eval(lhs, parent, parent) : 
  object 'Lab_Ash_Calculated' not found

# Import the Pol and Brix data into a data table called “Lab_PB_Data”

Lab_PB_Data <- read.table("Lab_Pol_Brix.csv", header=TRUE, sep=",", dec =".") 


# Use the mutate() function to add to the data table Lab_PB_Data a new variable, PredBrix, which uses the ExpectedBrix function with Pol as input

Lab_PB_Data <-mutate(Lab_PB_Data,PredBrix=ExpectedBrix(Pol))  


# Use ggplot() to visually display a scatter (i.e. point) plot of variables Brix and Pol. 
# Colour code the points according to if the absolute difference between the measured Brix (variable Brix, 3rd column) and the predicted Brix (variable PredBrix, 4th column) is greater than one. 

geom_point<-ggplot(data=Lab_PB_Data, mapping=aes(x=Brix, y=Pol)+geom_point(aes(col=abs(PredBrix-Brix)<1)))
Error: Mapping should be created with `aes() or `aes_()`.

Here's the file output for Lab_Ash_Data which contains 'InitialSampleInTinWeight':

dput(Lab_Ash_Data) 
structure(list(LabID = c(27L, 27L, 448L, 448L, 1041L, 1041L, 
1059L, 1059L, 1183L, 1183L, 2080L, 2080L, 2205L, 2205L, 2812L, 
2812L, 3288L, 3288L, 3916L, 3916L, 4047L, 4047L, 4148L, 4148L, 
4208L, 4208L, 4223L, 4223L, 4783L, 4783L, 4851L, 4851L, 5141L, 
5141L, 5359L, 5359L, 5521L, 5521L, 5531L, 5531L, 5687L, 5687L, 
5769L, 5769L, 6205L, 6205L, 6607L, 6821L, 6821L, 6919L, 6919L, 
6941L, 6941L, 7024L, 7024L, 7076L, 7076L, 7237L, 7237L, 7512L, 
7512L, 7632L, 7632L, 8180L, 8180L, 8980L, 8980L, 9688L, 9688L, 
9796L, 9796L, 9980L, 9980L, 10022L, 10022L, 10541L, 10541L, 10638L, 
10638L, 10798L, 10798L, 10874L, 10874L, 11671L, 11671L, 12718L, 
12718L, 12962L, 12962L, 13038L, 13038L, 13297L, 13297L, 13429L, 
13429L, 13501L, 13501L, 13892L, 13892L, 14117L, 14117L, 14501L, 
14501L, 14554L, 14554L, 15129L, 15129L, 15213L, 15213L, 15218L, 
15218L, 15237L, 15237L, 15325L, 15325L, 16145L, 16145L, 16223L, 
16223L, 16602L, 16602L, 16842L, 16842L, 17169L, 17169L, 17468L, 
17468L, 17938L, 17938L, 18100L, 18100L, 18818L, 18818L, 20482L, 
20482L, 20483L, 20483L, 20688L, 20688L, 21011L, 21011L, 21784L, 
21784L, 22631L, 22631L, 23077L, 23077L, 23445L, 23445L, 23769L, 
23769L, 23955L, 23955L, 24513L, 24513L, 24717L, 24717L, 24816L, 
24816L, 24854L, 24854L, 25084L, 25084L, 25160L, 25160L, 25209L, 
25209L, 25683L, 25683L, 25704L, 25704L, 26551L, 26551L, 26609L, 
26609L, 26696L, 26696L, 27151L, 27151L, 27181L, 27181L, 27866L, 
27866L, 27994L, 27994L, 28318L, 28318L, 28607L, 28607L, 28616L, 
28616L, 28939L, 28939L, 29318L, 29318L, 29799L, 29799L, 29825L, 
29825L, 30256L, 30256L, 30464L, 30464L, 30569L, 30569L, 31156L, 
31156L, 31478L, 31478L, 31709L, 31709L, 31854L, 31854L, 31929L, 
31929L, 32159L, 32159L, 32327L, 32327L, 32753L, 32753L, 33088L, 
33088L, 33545L, 33545L, 33697L, 33697L, 33876L, 33876L, 34803L, 
34803L, 35039L, 35039L, 35048L, 35048L, 35852L, 35852L, 35862L, 
35862L, 36045L, 36045L, 37320L, 37320L, 37819L, 37819L, 38297L, 
38297L, 38923L, 38923L, 39011L, 39011L, 39174L, 39174L, 39293L, 
39293L, 39437L, 39437L, 40066L, 40066L, 40331L, 40331L, 40413L, 
40413L, 40417L, 40417L, 40430L, 40430L, 40497L, 40497L, 40501L, 
40501L, 41029L, 41029L, 41541L, 41541L, 41580L, 41580L, 41596L, 
41596L, 42168L, 42168L, 42224L, 42224L, 42241L, 42241L, 43030L, 
43030L, 43449L, 43449L, 43843L, 43843L, 44123L, 44123L, 44461L, 
44461L, 44670L, 44670L, 45456L, 45456L, 45604L, 45604L, 45794L, 
45794L, 46009L, 46009L, 46361L, 46361L, 46476L, 46476L, 46945L, 
46945L, 46947L, 46947L, 47225L, 47225L, 47446L, 47446L, 47744L, 
47744L, 47862L, 47862L, 48422L, 48422L, 48498L, 48498L, 48893L, 
48893L, 49038L, 49038L, 49159L, 49159L, 49856L, 49856L, 49951L, 
49951L, 50473L, 50473L, 50763L, 50763L, 50858L, 50858L, 51416L, 
51416L, 51464L, 51464L, 51672L, 51672L, 51705L, 51705L, 51898L, 
51898L, 51930L, 51930L, 52174L, 52174L, 52174L, 52174L, 52244L, 
52244L, 52483L, 52483L, 52585L, 52585L, 53247L, 53247L, 53734L, 
53734L, 54062L, 54062L, 54718L, 54718L, 54804L, 54856L, 54856L, 
55019L, 55019L, 55055L, 55055L, 55492L, 55492L, 55772L, 55772L, 
55858L, 55858L, 56706L, 56706L, 57235L, 57235L, 57244L, 57244L, 
57448L, 57448L, 57687L, 57687L, 57879L, 57879L, 58295L, 58295L, 
58594L, 58594L, 58947L, 58947L, 59331L, 59331L, 59337L, 59337L, 
59459L, 59459L, 60268L, 60268L, 60337L, 60337L, 60693L, 60693L, 
60789L, 60789L, 61538L, 61538L, 61622L, 61622L, 62078L, 62078L, 
62234L, 62234L, 62282L, 62282L, 62543L, 62543L, 62653L, 62653L, 
62881L, 62881L, 63089L, 63089L, 63249L, 63249L, 63443L, 63443L, 
63531L, 63531L, 63725L, 63725L, 63730L, 63730L, 63767L, 63767L, 
63818L, 63818L, 63969L, 63969L, 64334L, 64334L, 64354L, 64354L
), TinWeight = c(425.2, 426.84, 428.77, 440.68, 426, 427.18, 
428.53, 441.57, 426.65, 426.61, 429.56, 442.14, 426.09, 426.67, 
429.41, 440.7, 426.79, 427.52, 428.85, 440.44, 426.41, 428.06, 
427.57, 441.45, 444.88, 435.29, 425.65, 428.22, 428.23, 441.57, 
425.29, 426.51, 426.87, 426.74, 427.87, 440.56, 428.07, 441.85, 
425.73, 427.28, 425.87, 426.49, 426.85, 427.17, 428.27, 442.31, 
425.33, 426.66, 428.03, 441.55, 426.11, 427.09, 429.1, 441.66, 
425.14, 428.11, 425.77, 427.29, 424.99, 426.93, 427.83, 440.86, 
426.33, 426.34, 427.68, 440.78, 425.18, 428.17, 428.05, 440.58, 
444.63, 435.44, 426.08, 426.79, 428.89, 441.43, 425.85, 427.43, 
426.27, 427.73, 426.6, 427.36, 428.41, 440.41, 425.96, 426.95, 
427.34, 440.21, 428.87, 440.97, 428.55, 441.6, 425.96, 427.18, 
443.82, 434.28, 426.01, 428.43, 425.62, 427.14, 428.71, 440.17, 
425.26, 426.76, 428.94, 441.41, 425.8, 427.01, 426.22, 428.43, 
444.31, 434.57, 428.63, 439.83, 426.55, 426.94, 425.44, 428.51, 
428.4, 440.05, 425.31, 427.36, 426.59, 426.88, 425.09, 428.26, 
427.64, 440.7, 426.41, 428.28, 428.48, 440.13, 444.25, 435.51, 
440.66, 428.23, 427.67, 440.57, 440.75, 428.5, 426.19, 427.69, 
441.06, 429.11, 428.37, 440.08, 424.79, 426.84, 441.27, 427.49, 
443.85, 436.65, 440.73, 428.09, 424.77, 427.04, 440.29, 428.14, 
440.28, 428.39, 424.54, 427.23, 435.28, 444.07, 424.99, 426.93, 
444.31, 436.1, 441.04, 428.3, 424.88, 427.11, 428.45, 441.4, 
444.54, 435.99, 424.82, 427.66, 426.51, 427.31, 427.49, 441.27, 
443.7, 436.38, 425.99, 427.72, 427.61, 441.07, 444.11, 435.13, 
424.62, 426.77, 427.58, 440.46, 444.23, 435.81, 426.1, 426.86, 
427.65, 440.89, 426.51, 426.92, 429.04, 440.44, 425.38, 427.84, 
429.28, 441.12, 425.22, 426.42, 425.18, 427.79, 427.53, 440.76, 
443.5, 436.53, 424.69, 427.78, 427.67, 440.66, 443.79, 436.05, 
427.13, 428.73, 441.97, 444.71, 436.44, 425.12, 427.51, 427.65, 
441.43, 423.89, 427.71, 428.07, 440.97, 444, 436.42, 426.03, 
427.45, 427.74, 441.69, 424.81, 426.2, 428.47, 441.56, 443.99, 
434.86, 425.14, 427.85, 429.17, 441.6, 426.46, 428.06, 429.32, 
442.05, 444.61, 436.24, 425.17, 426.84, 428.81, 440.69, 424.53, 
426.57, 426.89, 439.99, 425.14, 426.32, 427.65, 441.32, 424.47, 
426.82, 425.9, 425.53, 425.9, 426.38, 427.82, 440.96, 425.66, 
427.56, 428.75, 439.74, 424.9, 426.83, 426.99, 440.96, 425.23, 
426.81, 427.22, 440.66, 425.01, 427.39, 427.85, 440.23, 425.77, 
426.36, 427.72, 441.2, 425.37, 425.73, 424.42, 426.31, 425.32, 
427.06, 426.27, 426.65, 426.97, 441.35, 424.97, 426.24, 425.89, 
425.81, 427.76, 441.08, 424.32, 426.28, 426.17, 426.97, 424.46, 
426.2, 424.65, 426.31, 428.6, 441.28, 425.56, 425.8, 428.71, 
440.26, 425.3, 425.78, 428.31, 440.6, 425.87, 426.52, 426.12, 
426.56, 425.17, 425.99, 426.34, 427.62, 425.29, 426.89, 425.42, 
427.83, 424.45, 426.32, 427.68, 440.1, 425.89, 427.06, 427.48, 
440.8, 424.96, 425.81, 424.65, 426.93, 428.87, 441.69, 425.83, 
426.28, 428.1, 441.47, 425.81, 425.84, 427.65, 440.24, 424.82, 
426.1, 427.16, 440.69, 434.57, 443.23, 443.89, 434.67, 444.69, 
434.18, 444.98, 436.03, 444.99, 435.8, 426.73, 439.99, 444.51, 
434.38, 436.1, 444.02, 445.07, 435.06, 428.23, 439.5, 444.3, 
435.01, 444.34, 434.73, 441.11, 426.78, 434.74, 444.1, 445.06, 
434.43, 427.53, 439.86, 444.81, 435.32, 444.69, 434.73, 435.93, 
443.28, 435.81, 445.13, 443.9, 435.01, 427.79, 440.92, 426, 425.64, 
444.85, 435.34, 444.1, 434.63, 427.84, 439.9, 444.08, 434.79, 
435.72, 444.92, 440.07, 427.42, 435.48, 443.92, 441.33, 427.16, 
435.17, 443.89, 440.3, 427.26, 440.81, 427.3, 441.47, 427.61, 
440.03, 443.3, 435.87, 444.15, 434.57, 443.33, 434.88, 428.36, 
441.2, 443.57, 435.66, 427.95, 440, 435.78, 445.24), InitialSampleInTinWeight = c(638.22, 
642.11, 565.92, 569.51, 581.44, 611.96, 578.72, 600.77, 545.29, 
560.29, 586.57, 600.18, 586.04, 569.28, 563.82, 590.07, 553.66, 
556.59, 550.61, 561.4, 544.3, 564.17, 550.53, 575.93, 580.88, 
563.4, 685.21, 688.04, 559.48, 571.26, 569.51, 574.62, 719.53, 
706.97, 566.89, 580.83, 557.41, 579.87, 570.41, 581.58, 558.99, 
574.82, 589.3, 591.81, 567.09, 577.42, 738.82, 729.83, 691.63, 
683.86, 568.45, 577.91, 559.24, 572.2, 579.41, 574.78, 672.66, 
647.58, 626.78, 639.1, 661.73, 656.12, 678.42, 695.25, 698.92, 
730.69, 687.49, 665.05, 730.12, 639.99, 670.37, 657.29, 544.5, 
550.5, 662.71, 663.94, 647.71, 661.18, 652.44, 652.8, 543.19, 
556.97, 562.56, 585.57, 570.23, 543.37, 542.86, 559.78, 543.34, 
561, 695.66, 731.11, 720.02, 768.93, 588.05, 575.73, 704.02, 
691.27, 690.23, 0, 560.56, 579.22, 544.84, 544.52, 561.99, 576.41, 
714.08, 750.39, 728.17, 757.84, 592.2, 582.1, 565.34, 577.42, 
589.68, 574.35, 757.63, 779.88, 593.82, 596.65, 586.08, 565.63, 
571.83, 559.83, 566.52, 581.69, 575.81, 593.37, 575.83, 583.11, 
615.66, 612.14, 602.15, 599.01, 645.15, 631.51, 621.89, 617.63, 
628.1, 623.56, 613.88, 603.51, 645.28, 636.31, 600.93, 598.67, 
488.39, 497.8, 636.59, 606.56, 677.71, 646.05, 640.03, 634.21, 
600.17, 604.88, 617.13, 597.61, 636.41, 606.89, 581.25, 572.06, 
619.05, 622.12, 594.42, 593.55, 576.37, 604.95, 670.56, 659.04, 
586.26, 502.79, 594.11, 600.52, 614.1, 600.54, 598.85, 617.69, 
608.61, 611.29, 601.78, 608.88, 644.3, 640.64, 627.84, 620.5, 
646.76, 645.4, 632.93, 647.62, 643.69, 635.79, 646.47, 679.38, 
690.52, 650.24, 644.37, 660.07, 631.86, 645.24, 629.21, 612.85, 
627.65, 663.79, 640.18, 604.68, 557.06, 630.42, 647.59, 645.01, 
635.49, 633.74, 604.23, 640.98, 661.38, 627.3, 606.15, 609.65, 
625.54, 622.91, 656.05, 629.24, 630.19, 608.45, 633.79, 657.43, 
657.38, 652.77, 642.89, 626.66, 629.94, 633.11, 613.7, 629.03, 
646.17, 651.35, 646.45, 618.42, 615.41, 627.49, 634.88, 635.83, 
626.07, 652.18, 637.34, 684.17, 651.79, 620.82, 611.84, 633.16, 
640.96, 638.09, 663.71, 621.4, 640.66, 651.81, 674.47, 636.3, 
635.98, 620.75, 633.87, 539.67, 539.63, 545.41, 563.37, 578.68, 
579.66, 539.55, 554.1, 558.11, 577.09, 540.54, 550.61, 572.59, 
559.27, 541.37, 565.94, 554.41, 541.7, 558.14, 571.25, 548.57, 
548.89, 559.17, 562.69, 563.53, 580.35, 542.78, 546.17, 553.29, 
595.25, 585.52, 591.29, 544.94, 546.91, 600.05, 647, 564.16, 
594.76, 565.25, 576.33, 575.69, 582.02, 580.79, 546.22, 563.46, 
585.17, 550.92, 555.5, 537.78, 546.15, 574.06, 580.78, 553.55, 
547.66, 560.81, 562.06, 565.43, 566.04, 561.87, 559.88, 562.03, 
570.73, 586.18, 561.16, 577.69, 616.91, 557.01, 559.21, 563.18, 
569.45, 564.42, 553.72, 590.67, 580.39, 544.52, 549.2, 553.98, 
563.65, 546.35, 545.6, 546.83, 550.2, 567.64, 564.01, 560.29, 
568.8, 533.4, 550.37, 563.4, 593.05, 554.96, 546.41, 547.79, 
549.64, 548.39, 556.51, 561.4, 560.36, 566.26, 573.83, 537.88, 
549.29, 564.59, 584.6, 557.12, 573.82, 552.99, 559.26, 598.12, 
602.64, 586.44, 628.51, 556.02, 528.22, 580.23, 581.04, 566.83, 
558.73, 604.72, 571.39, 579.43, 564.96, 566.37, 612.86, 582.13, 
576.24, 541.36, 564.82, 575, 559.83, 567.31, 555.46, 592.02, 
594.63, 575.1, 571.98, 572.28, 595.24, 539.64, 545.42, 580.68, 
564.63, 586.02, 600.5, 570.37, 581.67, 543.96, 575.78, 604.49, 
587.91, 607.72, 577.29, 591.82, 564.47, 596.59, 571.47, 561.16, 
565.56, 588.88, 575.84, 589.57, 578.83, 570.9, 588.3, 558.56, 
549.05, 558.41, 568.06, 552.11, 532.45, 602.02, 587.33, 566.71, 
582.83, 541.03, 527.63, 570.58, 535.16, 542.56, 555.25, 552.66, 
551.39, 546.94, 560.34, 560.66, 555.42, 567.07, 573.76, 554.58, 
544.47, 549.91, 563.3, 564.33), FinalSampleInTinWeight = c(430.46, 
431.58, 434.09, 447.14, 432.01, 433.87, 431.16, 444.08, 429.47, 
430.89, 432.81, 446.08, 429.21, 429.91, 430, 442.92, 427.78, 
429.2, 431.29, 443.78, 427.72, 428.93, 430.78, 442.78, 445.42, 
437.98, 427.64, 430.58, 431.56, 443.86, 433.02, 433.8, 431.36, 
432.44, 434.49, 447.74, 430.71, 444.62, 427.68, 429.69, 428.41, 
430.43, 530.16, 431.79, 430.88, 443.54, 438.44, 440.33, 432.63, 
444.61, 429.49, 431.1, 430.11, 442.76, 429.21, 431.24, 432.97, 
433.2, 429.9, 432.35, 431.35, 444.72, 428.27, 430.48, 434.68, 
447.71, 434.08, 433.29, 432.2, 443.59, 448.46, 437.88, 427.27, 
428.46, 431.07, 443.2, 428.99, 429.89, 428.47, 429.69, 427.06, 
427.85, 429.83, 441.7, 428.66, 427.91, 429.63, 443.07, 428.94, 
441.37, 432.4, 444.99, 429.01, 432.88, 446.66, 437.5, 429.4, 
430.61, 429.23, 430.6, 432.23, 443.78, 428.14, 428.81, 429.85, 
443.03, 429.61, 431.64, 430.67, 432.65, 446.54, 437.9, 430.93, 
443.68, 427.76, 428.13, 429.32, 429.97, 435.56, 448.27, 431.43, 
432.24, 428.34, 429.51, 427.14, 428.29, 430.27, 443.12, 432.73, 
434.98, 429.99, 442.17, 445.75, 436.68, 443.66, 430.55, 432.72, 
445, 444.45, 432.21, 428.07, 429.85, 429.35, 429.76, 432.66, 
444.5, 426.55, 428.84, 445.53, 430.88, 450.97, 441.85, 443.76, 
432.07, 428.92, 430.58, 443.72, 430.98, 443.63, 430.33, 428.52, 
428.64, 440.29, 448.75, 428.11, 428.11, 446.33, 437.17, 443.02, 
429.91, 428.53, 427.8, 429.52, 442.39, 448.03, 438.04, 430.51, 
431.16, 426.54, 429.51, 430.18, 443.06, 446.94, 436.2, 426.61, 
429.48, 431.38, 442.57, 447.56, 439.25, 429.36, 430.05, 431.56, 
445.16, 446.78, 438.22, 429.05, 432.35, 431.57, 444.26, 427.2, 
430.25, 434.31, 447.43, 426.63, 429.23, 428.93, 444.12, 427.54, 
429.19, 428.29, 429.41, 430.52, 443.98, 446.64, 437.14, 429.28, 
430.45, 432.51, 444.66, 449.81, 439.15, 428.73, 430.77, 0, 447.36, 
438.46, 430.67, 433.17, 433.1, 445.01, 427.28, 429.58, 436.54, 
448.14, 445.51, 437.78, 427.23, 430.05, 430.42, 444.09, 430.72, 
430.71, 430.62, 443.08, 447.39, 439.4, 428.75, 0, 429.57, 442.68, 
429.07, 430.72, 429.72, 443.09, 446.58, 439.73, 428.73, 429.56, 
433.72, 446.4, 427.04, 427.05, 189.68, 198.68, 426.74, 428.2, 
429.99, 442.79, 426.32, 429.17, 427.69, 429.03, 186.24, 196.56, 
191.21, 443.16, 426.48, 427.23, 430, 442.1, 431.16, 431.62, 429.38, 
442.33, 427.27, 428.71, 429.17, 443.19, 426.44, 428.76, 428.27, 
442.58, 427.14, 426.78, 430.08, 442.37, 330.02, 331.86, 426.42, 
427.74, 427.53, 428.82, 428.32, 428.36, 430.38, 442.4, 427.18, 
427.79, 426.62, 427.07, 429.93, 443.21, 427.66, 427.67, 426.39, 
427.75, 428, 429.35, 428.78, 429.39, 430.17, 441.81, 426.97, 
428.68, 431.53, 445.26, 428.71, 428.98, 431.17, 443.54, 426.85, 
428.92, 427.21, 429.24, 427, 428.92, 426.96, 429.42, 425.96, 
428.03, 427.84, 429.49, 426.53, 429.04, 430.25, 443.79, 428.1, 
429.75, 429.89, 442.96, 428.44, 429.42, 428.19, 429.59, 428.89, 
442.91, 427.2, 428.74, 429.37, 441.84, 427.32, 429.71, 429.98, 
443.52, 426.6, 429.45, 432.81, 444.31, 438.1, 445.72, 446.62, 
436.9, 447.35, 437.17, 447.02, 438.75, 445.96, 436.2, 429.82, 
442.02, 448.67, 439.15, 437.66, 447.66, 445.94, 436.74, 427.82, 
441.88, 445.23, 438.01, 445.2, 435.69, 442.97, 429.96, 437.39, 
446.06, 445.15, 437.34, 430.27, 441.31, 447.14, 438.24, 446.39, 
438.21, 437.13, 445.8, 435.99, 445.6, 445.39, 437.09, 429.76, 
442.57, 428.8, 426.56, 445.59, 436.67, 445.01, 436.4, 431.05, 
442.87, 445.82, 436.49, 437.45, 445.69, 441.47, 428.33, 435.9, 
445.98, 441.85, 427.91, 438.37, 447.68, 442.57, 428.74, 440.71, 
428.75, 442.88, 431, 443.1, 445.87, 436.55, 446, 436.7, 445.46, 
436.46, 428.87, 442.88, 445.91, 437.33, 430.96, 443.13, 436.42, 
444.61)), class = "data.frame", row.names = c(NA, -454L))

You have

Lab_Ash_Calculated$Ash <- Lab_Ash_Data %>% 
  filter(InitialSampleInTinWeight ==0 & FinalSampleInTinWeight ==0) & 
  mutate(InitialWeight = InitialSampleInTinWeight) & 
  mutate(FinalWeight = FinalSampleInTinWeight) & 
  mutate(Ash = 100 *(FinalWeight/InitialWeight))

You pass Lab_Ash_Data into the filter() function but after that function you have an &. Do you mean to have %>% there to pass that output to the following mutate()? You also have & after the mutates, where I would expect a %>%.

By the way, arguments separated with commas within filter() are connected with logical AND, so you could write

filter(InitialSampleInTinWeight ==0, FinalSampleInTinWeight ==0)
1 Like

Thanks for the feedback.

I need to filter out zero values before calculating the percentage ash by using a PIPE to:

(a) first filter out the missing values using filter()

then sequentially calculate:
(b) “InitialWeight”,
(c) “FinalWeight”
(d) “Ash” as new variables using mutate().

So they need to be be calculated separately, no output passing.

What do I need to do differently so that the object 'InitialSampleInTinWeight' is found? Seems strange that it's able to find the other variables in the same file.

Here are two equivalent solutions.

Lab_Ash_Calculated <- Lab_Ash_Data %>% 
  filter(InitialSampleInTinWeight ==0 & FinalSampleInTinWeight ==0) %>% 
  mutate(InitialWeight = InitialSampleInTinWeight) %>% 
  mutate(FinalWeight = FinalSampleInTinWeight) %>% 
  mutate(Ash = 100 *(FinalWeight/InitialWeight))
Lab_Ash_Calculated <- Lab_Ash_Data %>% 
  filter(InitialSampleInTinWeight ==0 & FinalSampleInTinWeight ==0)
Lab_Ash_Calculated <- Lab_Ash_Calculated %>% mutate(InitialWeight = InitialSampleInTinWeight)  
Lab_Ash_Calculated <- Lab_Ash_Calculated %>% mutate(FinalWeight = FinalSampleInTinWeight)
Lab_Ash_Calculated <- Lab_Ash_Calculated %>% mutate(Ash = 100 *(FinalWeight/InitialWeight))

In either case, Lab_Ash_Calculated looks like Lab_Ash_Data with the zeros filtered out and three new columns, InitialWeight, FinalWeight, and Ash. If you do not want all of the columns, you can choose the ones you want with select().
Note that in no case did I try to assign to a column on the left hand side as you did with
Lab_Ash_Calculated$Ash. The filter() and mutate() functions return whole data frames, not a column.
Also, bear in mind that

Lab_Ash_Calculated %>% mutate(InitialWeight = InitialSampleInTinWeight)

is the equivalent of

mutate(Lab_Ash_Calculated, InitialWeight = InitialSampleInTinWeight)

The %>% sends its left hand side as the first argument of its right hand side. Your

mutate(InitialWeight = InitialSampleInTinWeight)

failed because no proper first argument is provided; there is no preceding %>% and InitialWeight = InitialSampleInTinWeight is not a data frame that the function can act on.

Edit: Looking at the code, the first two mutates are unnecessary. Just calculate Ash from InitialSampleInTinWeight and FinalSampleInTinWeight.

1 Like

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