Search code examples
rdplyrtidyverseplyr

Error while Calculating Weighted Standard deviation in R


i am trying to calculate weighted standard deviation in R using the following code

library(dplyr)
library(matrixStats)
Output3 = Output2 %>%
  group_by(ProductionBatchNo) %>%
  summarize(
 across(as.numeric(`SDActVisc`)), 
 WtdSD = round(weightedSd(x=SDActVisc,w=Counts),2), 
 OverallCounts = n()
  )

I am getting the following error:

Error in FUN(X, ...) : argument "FUN" is missing, with no default

I am not able to understand what went wrong.


Solution

  • Difficult to now for sure without any data but this should work:

    Output2 %>%
      group_by(ProductionBatchNo) %>%
      summarize(
        WtdSD = round(weightedSd(x = as.numeric(SDActVisc), w = Counts), 2), 
        OverallCounts = n(), .groups = "drop"
        )
    

    What are you trying to do with the across function? You are not using it correctly anyways.