Error Message dplyr

Hello

When running this code

</>
Distinct_NPC <- df1 %>% distinct(NPC)

for (j in 1:nrow(Distinct_NPC))
{

Distinct_PAIS <- df1 %>% filter(NPC==Distinct_NPC[j,]) %>% distinct(PAIS_COD)

for(i in 1:nrow(Distinct_PAIS))
{
A <- df1 %>% filter(NPC==Distinct_NPC[j,] & PAIS_COD==Distinct_PAIS[i,]) %>% group_by(ANO_REF,MES_REF) %>% summarise(VF = sum(VF))
N <- nrow(A)

if(N!=0) {
  B <- A$VF
  
  NPC <- Distinct_NPC[j,]
  PAIS_COD <- Distinct_PAIS[i,]
  NC8 <- ""
  
  MAD <- c(median(B)-2*mad(B), median(B)+2*mad(B))
  Lim_Inf <- MAD[1]
  Lim_Sup <- MAD[2]
  
  C <- df1 %>% filter(NPC==Distinct_NPC[j,] & PAIS_COD==Distinct_PAIS[i,] & MES_REF==1) %>% group_by(MES_REF) %>% summarise(VF = sum(VF))
  D <- nrow(C)
  ifelse(D==0, VF <- 0, VF <- C$VF)
  
  Res_2MAD <- if(VF<MAD[1]|VF>MAD[2]) {Res_2MAD <- "outlier"} else{Res_2MAD <- "normal"}
  
  New <- data.frame(NPC, PAIS_COD, NC8, VF, Lim_Inf, Lim_Sup, Res_2MAD, N)
  
  if(j==1 & i==1) {NPC_PAIS_VF <- data.frame(NPC, PAIS_COD, NC8, VF, Lim_Inf, Lim_Sup, Res_2MAD, N)}
  else {NPC_PAIS_VF <- rbind.data.frame(NPC_PAIS_VF, New)}
  
}

}
}
</>

I've obtained this error message:

summarise() regrouping output by 'ANO_REF' (override with .groups argument)
Error: Problem with filter() input ..1.
x dims [product 1] do not match the length of object [815358]
i Input ..1 is &....
Run rlang::last_error() to see where the error occurred.

rlang::last_error()
<error/dplyr_error>
Problem with filter() input ..1.
x dims [product 1] do not match the length of object [815358]
i Input ..1 is &....
Backtrace:

  1. dplyr::filter(...)
  2. dplyr:::h(simpleError(msg, call))
    Run rlang::last_trace() to see the full context.

rlang::last_trace()
<error/dplyr_error>
Problem with filter() input ..1.
x dims [product 1] do not match the length of object [815358]
i Input ..1 is &....
Backtrace:
x

  1. +-%>%(...)
  2. | +-base::withVisible(eval(quote(_fseq(_lhs)), env, env))
  3. | -base::eval(quote(_fseq(_lhs)), env, env)
  4. | -base::eval(quote(_fseq(_lhs)), env, env)
  5. | -_fseq(_lhs)
  6. | -magrittr::freduce(value, _function_list)
  7. | -function_list[i]
  8. | +-dplyr::filter(...)
  9. | -dplyr:::filter.data.frame(...)
  10. | -dplyr:::filter_rows(.data, ...)
  11. | +-base::withCallingHandlers(...)
  12. | -mask$eval_all_filter(dots, env_filter)
  13. -base::.handleSimpleError(...)
  14. -dplyr:::h(simpleError(msg, call))

Can you help me please?

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