Error Message while running code

Hello

I have this error message

´´´
Error: Problem with filter() input ..1.
x dims [product 3] do not match the length of object [12]
i Input ..1 is &....
i The error occurred in group 1: NPC = 103920617, PAIS_COD = "ES", ANO_REF = 2016.
´´´

Can you help me?

Hello @ramgouveia,

In order to be of any assistance to your request, could you provide us with a reprex of your issue?

1 Like

Thank you Lars

Here it is

structure(list(NPC = c(103920617L, 103920617L, 103920617L, 103920617L,
103920617L, 103920617L, 103920617L, 103920617L, 103920617L, 103920617L,
103920617L, 103920617L, 106320750L, 106320750L, 106320750L, 106320750L,
106320750L, 106320750L, 106320750L, 106320750L, 106320750L, 106320750L,
106320750L, 106320750L, 106320750L, 106320750L, 106320750L, 106320750L,
106320750L, 106320750L, 106320750L, 106320750L, 106320750L, 106320750L,
106320750L, 106320750L, 106320750L, 106320750L, 106320750L, 106320750L,
106320750L, 106320750L, 106320750L, 106320750L, 106320750L, 106320750L,
106320750L, 106320750L, 106320750L, 106320750L), PAIS_COD = c("ES",
"ES", "ES", "ES", "ES", "ES", "ES", "ES", "ES", "ES", "ES", "ES",
"AT", "BE", "BE", "BE", "BE", "BE", "BE", "BE", "BE", "BE", "BE",
"BE", "BE", "BE", "BE", "BE", "BE", "BE", "BE", "CZ", "CZ", "CZ",
"DE", "DE", "DE", "DE", "DE", "DE", "DE", "DE", "DE", "DE", "DE",
"DE", "DE", "DE", "DE", "DE"), ANO_REF = c(2016L, 2016L, 2016L,
2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L,
2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 2018L,
2018L, 2018L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L,
2017L, 2018L, 2017L, 2016L, 2018L, 2018L, 2018L, 2018L, 2018L,
2018L, 2018L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L,
2017L, 2017L), MES_REF = c(12L, 11L, 10L, 9L, 8L, 7L, 6L, 5L,
4L, 3L, 2L, 1L, 6L, 12L, 11L, 10L, 9L, 8L, 7L, 5L, 3L, 2L, 1L,
12L, 11L, 9L, 8L, 4L, 3L, 2L, 1L, 11L, 10L, 11L, 12L, 10L, 9L,
5L, 4L, 3L, 1L, 12L, 11L, 10L, 8L, 7L, 5L, 4L, 3L, 2L), VF = c(14297,
17326, 33461, 37192, 39201, 35816, 25126, 28974, 16166, 14058,
11517, 5381, 2526, 11664, 11536, 10, 4404, 23688, 8103, 2266,
2485, 3749, 22098, 9488, 4721, 72464, 5654, 32610, 4199, 27630,
27572, 3362, 3488, 1799, 1010, 456, 16635, 3922, 3238, 16083,
3828, 20012, 6977, 6857, 2487, 837, 2664, 5335, 6817, 7873),
NPCPAISCOD = c("103920617ES", "103920617ES", "103920617ES",
"103920617ES", "103920617ES", "103920617ES", "103920617ES",
"103920617ES", "103920617ES", "103920617ES", "103920617ES",
"103920617ES", "106320750AT", "106320750BE", "106320750BE",
"106320750BE", "106320750BE", "106320750BE", "106320750BE",
"106320750BE", "106320750BE", "106320750BE", "106320750BE",
"106320750BE", "106320750BE", "106320750BE", "106320750BE",
"106320750BE", "106320750BE", "106320750BE", "106320750BE",
"106320750CZ", "106320750CZ", "106320750CZ", "106320750DE",
"106320750DE", "106320750DE", "106320750DE", "106320750DE",
"106320750DE", "106320750DE", "106320750DE", "106320750DE",
"106320750DE", "106320750DE", "106320750DE", "106320750DE",
"106320750DE", "106320750DE", "106320750DE"), num = c(1L,
2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L, 1L, 2L,
3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L,
16L, 17L, 18L, 1L, 2L, 3L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L,
9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L)), row.names = c(NA,
-50L), groups = structure(list(NPC = c(103920617L, 106320750L,
106320750L, 106320750L, 106320750L, 106320750L, 106320750L, 106320750L,
106320750L), PAIS_COD = c("ES", "AT", "BE", "BE", "CZ", "CZ",
"CZ", "DE", "DE"), ANO_REF = c(2016L, 2018L, 2017L, 2018L, 2016L,
2017L, 2018L, 2017L, 2018L), .rows = structure(list(1:12, 13L,
24:31, 14:23, 34L, 33L, 32L, 42:50, 35:41), ptype = integer(0), class = c("vctrs_list_of",
"vctrs_vctr", "list"))), row.names = c(NA, 9L), class = c("tbl_df",
"tbl", "data.frame"), .drop = TRUE), class = c("grouped_df",
"tbl_df", "tbl", "data.frame"))

Distinct_NPC <- F2_2016_2018 %>% distinct(NPC)

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

  Distinct_PAIS <- F2_2016_2018 %>% filter(NPC==Distinct_NPC[j,]) %>% distinct(PAIS_COD)
  
  for(i in 1:nrow(Distinct_PAIS))
    {
    A <- F2_2016_2018 %>% 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 <- F2_2016_2018 %>% 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)}
  
      }
    }
  }

This is the code.

Hi ramgouveia,

Since I got familiar with the functional programming approach, I try to avoid for() loops as much as possible. In case you're interested, I can highly recommend chapter 21 of the excellent book

Anyway, it would help a lot if you can describe the desired result or provide a sketch of the expected output.

Thank you @lars

I also have that book but I haven't read it yet.

If I continue need your help I will call you.

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.