I am trying to modify existing code that calculates the mean of results within a dataframe and then assesses them over different averaging periods. First off, I would really love some help breaking down exactly what the existing chunk of code is doing since I am not familiar with the as.data.table function. Here is the existing code:
W_Objectives<-as.data.table(W_Objectives)[,mean(Result),list(AnalyteName,BeneficialUse
,UnitName,StationCode,ProjectName,SampleDate,MatrixName,FractionName
,TargetLatitude,TargetLongitude,Waterbody,WBID,Wbtype,Objective,AveragingPeroid
,Objective_Language,Evaluation_Guideline,Objective_Ref_Number,Eval_Ref_Number, Comment)]
names(W_Objectives)[names(W_Objectives)=='V1']<-"Result"
W_Objectives<-tbl_df(W_Objectives)
I am trying to pull specific data out of my dataframe to average on a monthly basis instead. I did this like so:
monthlymean<-W_Objectives[W_Objectives$Comment == "monthly mean",]
monthlymean <- monthlymean %>%
mutate(
year=year(SampleDate), # extract parts
month=month(SampleDate)
)
My first question is: should I extract the data to be evaluated by a monthly mean prior to running the first chunk?
My second question is regarding how I would format a monthly mean using the as.data.table function. I was originally planning on using the aggregate function using month and year, but if there is an easy way to do this using as.data.table it would keep the code more streamlined for future use.
I appreciate any guidance as I am fairly new to R!