dplyr group_by and .groups

When R executed the following comand

A <- F2_2016_2018 %>% filter(NPC==Distinct_NPC[j,] & PAIS_COD==Distinct_PAIS[i,]) %>% group_by(ANO_REF,MES_REF) %>% summarise(VF = sum(VF))

summarise() has grouped output by 'ANO_REF'. You can override using the .groups argument.

What is the .groups argument?

How can I rewrite the upper comand?

Thank you.

.groups is an argument in summarise()
See

?summarise

I think we need more information about what you are doing before anyone can comment on a rewrite of the command.

Why?

Can you supply some sample data? dput() format would be great. Am I correct that you are referencing two data.frames in that loop?

jrkrideau

structure(list(ANO_REF = c(2016L, 2016L, 2016L, 2016L, 2016L,
2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L,
2016L), MES_REF = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L), FLUXO = c(2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L), NPC = c(505585049L, 980347424L, 506731421L,
501335668L, 505367190L, 507610865L, 504514709L, 510489575L, 500192480L,
507232330L, 500774846L, 507629213L, 506697142L, 510676049L, 505145006L
), PAIS_COD = c("TT", "TT", "TT", "TT", "TT", "TT", "TT", "TT",
"TT", "TT", "TT", "TT", "TT", "TT", "TT"), NC8 = c(0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), VF = c(0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), ML = c(0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0), US = c(0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0)), row.names = c(NA, 15L), class = "data.frame")

Here is de dput and i'm trying to define a interval centered on Median for a combination of NPC and PAIS_COD

This makes no sense given the sample data. PAIS_COD has only one value.

Also

Distinct_NPC <- dat1 %>% distinct(NPC)

does not make any sense as all it does is convert NPC from a vector to a 1 column data.frame since there are no duplicates in NPC.

Perhaps we need a more representative data set?

jrkrideau

The data set has more than two million rows with at least 30 different PAIS_COD registers and lots of NPC different records.

But send a greater sample from the data set has a greater code. Which is the maximum number of records I can share?

Thank you.

Ah I suspected something like that.

I am fairly new on this forum so I am not sure how many records you can share but I imagine maybe 600 would be acceptable though we probably do not need that many. Can you randomly sample from the database to get a bit of variety? The main thing would seem to be just to have at least 2 values for the PAIS_COD & NPC vectors though more would be nice.

jrkrideau

Here are 300 rows.

structure(list(ANO_REF = c(2016L, 2016L, 2016L, 2016L, 2016L, 
2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 
2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 
2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 
2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 
2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 
2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 
2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 
2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 
2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 
2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 
2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 
2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 
2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 
2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 
2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 
2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 
2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 
2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 
2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 
2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 
2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 
2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 
2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 
2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 
2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 
2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 
2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 
2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 
2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 
2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 
2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 
2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 
2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L), MES_REF = c(1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L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 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), NPC = c(505585049L, 
980347424L, 506731421L, 501335668L, 505367190L, 507610865L, 504514709L, 
510489575L, 500192480L, 507232330L, 500774846L, 507629213L, 506697142L, 
510676049L, 505145006L, 509220665L, 500276870L, 510345280L, 507610938L, 
505469464L, 500981507L, 507395026L, 502560118L, 510714650L, 503030708L, 
509825621L, 501532889L, 510730566L, 980174759L, 503240699L, 503462497L, 
505365146L, 503948640L, 503361135L, 501411305L, 508384427L, 500443025L, 
501887946L, 500449686L, 511158955L, 503751804L, 510465293L, 506629244L, 
503415588L, 510896103L, 502414669L, 500812128L, 504109820L, 145951235L, 
507775597L, 508099978L, 508446074L, 508638631L, 510689353L, 505866226L, 
505270153L, 509894178L, 507740750L, 500106878L, 501220968L, 503899640L, 
509211313L, 502466219L, 503432750L, 509846556L, 501997458L, 500236178L, 
503270075L, 504919300L, 509427855L, 502573627L, 504868713L, 502185830L, 
508369070L, 501999574L, 502560967L, 510010903L, 500150575L, 501920277L, 
509699103L, 502180200L, 510811876L, 504721658L, 509412432L, 144773791L, 
513352058L, 501286985L, 509257470L, 506561356L, 507900537L, 508155266L, 
501150315L, 510762557L, 502728264L, 502897864L, 980308429L, 506386333L, 
510693520L, 980487684L, 505955342L, 513250360L, 510634036L, 510255450L, 
500331251L, 507972600L, 503670650L, 505291940L, 503213748L, 500236364L, 
505833093L, 500508291L, 501080317L, 502187336L, 504947184L, 506502740L, 
508736641L, 501877878L, 508173221L, 509074138L, 508140056L, 503802484L, 
505342910L, 509842860L, 103920617L, 106320750L, 106320750L, 106320750L, 
108933660L, 118375008L, 118474650L, 118474650L, 120105721L, 122120647L, 
122120647L, 122211146L, 122211146L, 122211146L, 122244648L, 122244648L, 
122842081L, 122842081L, 123583977L, 123704200L, 123704200L, 123704200L, 
123704200L, 123704200L, 123704200L, 123704200L, 123704200L, 124016308L, 
124535500L, 124625843L, 124625843L, 124697461L, 133680665L, 134339533L, 
134339533L, 134339533L, 134339533L, 134339533L, 134339533L, 134343166L, 
134343166L, 134343166L, 137793626L, 137793626L, 137914016L, 138290350L, 
138332738L, 141045035L, 141045035L, 141045035L, 152105301L, 158889924L, 
162870027L, 166464147L, 169406440L, 171354869L, 171778774L, 171778774L, 
171778774L, 171778774L, 171778774L, 171778774L, 171778774L, 171778774L, 
171778774L, 171778774L, 171778774L, 171778774L, 171778774L, 171778774L, 
172596238L, 176285350L, 179761730L, 180091379L, 180091379L, 181157446L, 
181512530L, 181512530L, 184374774L, 184374774L, 186421400L, 186421400L, 
186421400L, 190228032L, 190228032L, 190228032L, 190228032L, 190228032L, 
190480289L, 190480289L, 192396145L, 192759566L, 193764083L, 193764083L, 
193764083L, 193764083L, 193764083L, 194465632L, 196837154L, 196837154L, 
196837154L, 196837740L, 196837740L, 196837740L, 204982987L, 204982987L, 
206449313L, 206449313L, 206449313L, 206449313L, 208477500L, 208727213L, 
208727213L, 208727213L, 208727213L, 208727213L, 208727213L, 208727213L, 
217930476L, 217930476L, 221401024L, 225559331L, 225559331L, 225559331L, 
226085856L, 227522001L, 227522001L, 228083923L, 232762589L, 236414569L, 
236414569L, 236414569L, 236414569L, 236414569L, 236414569L, 245358846L, 
247745650L, 250866951L, 277650542L, 280009887L, 280009887L, 280009887L, 
280009887L, 280009887L, 280009887L, 500000026L, 500000026L, 500000026L, 
500000026L, 500000026L, 500000026L, 500000026L, 500000026L, 500000026L, 
500000026L, 500000026L, 500000026L, 500000026L, 500000026L, 500000026L, 
500000026L, 500000026L, 500000026L, 500000026L, 500000026L, 500000026L, 
500000026L, 500000026L, 500000573L, 500000573L, 500000573L, 500000573L, 
500000573L, 500000573L, 500000573L, 500000573L, 500000573L), 
    PAIS_COD = c("TT", "TT", "TT", "TT", "TT", "TT", "TT", "TT", 
    "TT", "TT", "TT", "TT", "TT", "TT", "TT", "TT", "TT", "TT", 
    "TT", "TT", "TT", "TT", "TT", "TT", "TT", "TT", "TT", "TT", 
    "TT", "TT", "TT", "TT", "TT", "TT", "TT", "TT", "TT", "TT", 
    "TT", "TT", "TT", "TT", "TT", "TT", "TT", "TT", "TT", "TT", 
    "TT", "TT", "TT", "TT", "TT", "TT", "TT", "TT", "TT", "TT", 
    "TT", "TT", "TT", "TT", "TT", "TT", "TT", "TT", "TT", "TT", 
    "TT", "TT", "TT", "TT", "TT", "TT", "TT", "TT", "TT", "TT", 
    "TT", "TT", "TT", "TT", "TT", "TT", "TT", "TT", "TT", "TT", 
    "TT", "TT", "TT", "TT", "TT", "TT", "TT", "TT", "TT", "TT", 
    "TT", "TT", "TT", "TT", "TT", "TT", "TT", "TT", "TT", "TT", 
    "TT", "TT", "TT", "TT", "TT", "TT", "TT", "TT", "TT", "TT", 
    "TT", "TT", "TT", "TT", "TT", "ES", "BE", "DE", "DE", "ES", 
    "ES", "ES", "GB", "ES", "ES", "ES", "ES", "ES", "ES", "ES", 
    "ES", "ES", "FR", "FR", "ES", "ES", "ES", "ES", "ES", "ES", 
    "FR", "FR", "GB", "ES", "FR", "FR", "ES", "ES", "ES", "FI", 
    "FI", "SE", "SE", "SK", "ES", "FR", "IT", "ES", "ES", "ES", 
    "ES", "ES", "ES", "ES", "ES", "ES", "FR", "ES", "FR", "ES", 
    "ES", "AT", "BE", "CZ", "DE", "ES", "FR", "GB", "GR", "HU", 
    "IT", "LV", "NL", "PL", "SE", "ES", "ES", "ES", "FR", "FR", 
    "FR", "ES", "ES", "FR", "FR", "DK", "FR", "SE", "ES", "ES", 
    "ES", "ES", "ES", "FR", "FR", "ES", "BE", "FR", "FR", "FR", 
    "FR", "FR", "ES", "DK", "ES", "GB", "ES", "ES", "ES", "DK", 
    "GB", "ES", "ES", "ES", "ES", "FR", "ES", "ES", "ES", "ES", 
    "ES", "ES", "ES", "ES", "NL", "ES", "BE", "FR", "NL", "NL", 
    "ES", "ES", "ES", "ES", "BE", "BE", "BE", "DE", "DE", "DE", 
    "ES", "ES", "NL", "FR", "ES", "ES", "ES", "ES", "ES", "ES", 
    "BE", "BE", "DE", "DE", "DE", "DK", "EE", "ES", "FI", "FR", 
    "GB", "GB", "GB", "IE", "IT", "LU", "LU", "LV", "NL", "NL", 
    "NL", "PL", "SE", "DK", "ES", "ES", "ES", "FR", "FR", "IT", 
    "LT", "LU"), NC8 = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 7051900L, 22042169L, 
    22042138L, 22042169L, 7099290L, 7099290L, 69149000L, 69149000L, 
    1022961L, 41015050L, 41022900L, 8021190L, 12129200L, 12129949L, 
    44013930L, 44071093L, 25169000L, 25169000L, 76101000L, 40111000L, 
    73029000L, 87032110L, 87087050L, 87089435L, 87149990L, 87032110L, 
    87149990L, 44160000L, 52021000L, 73083000L, 76101000L, 3077100L, 
    39235090L, 73269098L, 72189920L, 73269098L, 72189920L, 73269098L, 
    72189920L, 8024100L, 8024100L, 8024100L, 33072000L, 34011100L, 
    45019000L, 1022991L, 1022941L, 3019410L, 3024700L, 3028130L, 
    1022941L, 73143900L, 84199085L, 76101000L, 7020000L, 73089059L, 
    87149990L, 87149990L, 87149990L, 87149990L, 87149990L, 87149990L, 
    87149990L, 87149990L, 87149990L, 87149990L, 87149990L, 87149990L, 
    87149990L, 87149990L, 44092999L, 3025990L, 69120029L, 7099390L, 
    8083090L, 64039118L, 7032000L, 7051900L, 25161100L, 69149000L, 
    42032100L, 42031000L, 42031000L, 17049071L, 19053191L, 20052020L, 
    21041000L, 34021200L, 63029100L, 69120025L, 7099290L, 38151200L, 
    72166110L, 73083000L, 73089051L, 73143100L, 73145000L, 1029099L, 
    73211110L, 73211110L, 73211110L, 7020000L, 7039000L, 7051900L, 
    42031000L, 42031000L, 3022300L, 3074110L, 3075100L, 3077100L, 
    62079990L, 3022200L, 3022300L, 3025411L, 3028200L, 3028410L, 
    3074110L, 3075100L, 7095990L, 7095990L, 8051080L, 7095100L, 
    8102010L, 8102010L, 6031200L, 1012910L, 1022961L, 44152020L, 
    1039219L, 87033190L, 87042139L, 87042199L, 87033190L, 87042139L, 
    87042199L, 68021000L, 1029091L, 6031100L, 97011000L, 20091200L, 
    20093151L, 20097911L, 22021000L, 22030001L, 22030010L, 22042179L, 
    22042189L, 22042169L, 22042180L, 22042189L, 22042189L, 22042189L, 
    22042189L, 22042189L, 22042189L, 22042138L, 22042180L, 22042189L, 
    22042189L, 22042189L, 22042169L, 22042189L, 22042189L, 22042169L, 
    22042180L, 22042189L, 22042189L, 22042189L, 19012000L, 15171090L, 
    17049051L, 19012000L, 15171090L, 19012000L, 19012000L, 19012000L, 
    15171090L), VF = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5381, 1901, 
    1032, 4693, 12158, 65362, 515, 3440, 26855, 11971, 10979, 
    19915, 77302, 348315, 261, 59487, 20820, 2662, 70538, 843, 
    22, 6000, 1710, 236, 1925, 6210, 645, 21300, 4425, 4158, 
    85136, 12480, 25650, 390, 520, 10701, 6786, 14000, 326, 88571, 
    73460, 32700, 7862, 31933, 17137, 3674, 26100, 9068, 5009, 
    39300, 132613, 81480, 218, 15574, 26548, 31590, 109, 822, 
    472, 6041, 26047, 11706, 6287, 329, 182, 4099, 485, 795, 
    1662, 287, 32335, 4778, 600, 13744, 9118, 10374, 5607, 44867, 
    3200, 32130, 2868, 7786, 19687, 2583, 13148, 6966, 2530, 
    1987, 89, 2297, 88802, 100835, 7447, 9890, 250, 13760, 932, 
    48784, 168, 3041, 4049, 19997, 5514, 33448, 250, 34723, 577, 
    70, 7859, 9630, 205298, 271, 15346, 417, 4635, 2362, 1674, 
    8351, 10705, 68598, 185113, 3684, 11949, 31235, 50357, 2662, 
    68700, 37438, 30989, 3600, 13450, 15550, 350, 1250, 6700, 
    28068, 39860, 3294, 115000, 8068, 5443, 5395, 15693, 14147, 
    6874, 17678, 684, 2400, 1807, 98053, 8846, 4180, 36209, 12474, 
    53356, 585, 525, 38983, 6367, 5323, 2976, 64533, 2253, 4618, 
    960, 220885, 4083, 7415, 1181, 618, 5205, 53612, 177, 528, 
    9723, 11350, 334), ML = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7250, 
    432, 360, 663, 25330, 139144, 2400, 6300, 11447.5, 59928, 
    659, 22050, 475660, 126660, 6380, 232794, 137264, 4746, 5470, 
    80, 0.5, 300, 84, 3, 127.5, 300, 450, 7300, 3789, 310.98, 
    5391.9, 2250, 2590, 1.3, 59, 386, 156, 704, 1, 51205, 34030, 
    15000, 727.92, 10137.6, 122406, 1790.46, 9600, 1810, 590, 
    22910, 57958, 45000, 2.3, 1670, 36450, 18605, 6, 39, 23, 
    288, 1241, 558, 300, 16, 9, 196, 23, 38, 80, 14, 100237.26, 
    526, 1400, 49287, 11427, 469, 8850, 43200, 1500, 23800, 28, 
    51, 512, 1096.4, 7294.8, 1593, 501.6, 1255.68, 5.93, 458.47, 
    205668, 2125, 2406, 998, 160, 1300, 210, 33387, 600, 16800, 
    33200, 17116, 7800, 37945, 2, 283, 45.3, 17.6, 1455.4, 2554, 
    24240, 52, 1269, 244, 495, 779, 385, 1967, 3225.5, 9308, 
    504929, 476.5, 1404, 3042, 11678, 2136, 26980, 311788, 16800, 
    4000, 18120, 16600, 1085, 2480, 5825, 32023, 15509.88, 1134, 
    158, 4980, 3360, 3330, 9240, 9023, 5400, 4186, 64, 1148, 
    1033, 16154, 2480, 918, 15008, 716, 15102, 229.5, 229.5, 
    4716, 1584, 482, 367, 13879, 482, 1261.8, 573.75, 41680, 
    459, 1597, 966, 510, 1994, 29878, 100, 215, 5960, 5832, 200
    ), US = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 432, 360, 664, 0, 
    0, 0, 0, 38, 204, 1882, 0, 0, 0, 0, 401, 0, 0, 64, 16, 0, 
    1, 0, 0, 0, 1, 0, 0, 0, 4, 90, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 5, 48, 0, 0, 0, 196, 0, 0, 36, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 503, 0, 
    0, 0, 0, 620, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 17, 0, 
    0, 0, 93, 1, 28, 51, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 242020, 12, 71, 13556, 140, 
    4, 9, 10, 1, 1, 4, 0, 54, 9454, 0, 0, 0, 0, 9240, 9023, 5400, 
    4104, 63, 1125, 1012, 15836, 2325, 900, 14713, 702, 14805, 
    225, 225, 4623, 1552, 472, 360, 13607, 472, 1237, 562, 40861, 
    450, 1566, 0, 0, 0, 0, 0, 0, 0, 0, 0)), row.names = c(NA, 
300L), class = "data.frame")

I think you have again only provided an example with a single PAIS_COD
using tidyverse you can create a dataframe which contains example records from groups.
Here is an example using the built in iris dataset that has 150rows , 50 of each Species group.
My example should how to get the first 5 records of each of the 3 species, for a resulting table of 15rows.

library(tidyverse)
iris %>%
 group_by(Species) %>%
 slice(1:5)
1 Like

nirgrahamuk

Thank you for your useful sugestion but my 300 rows dataset has different PAIS_COD registers.

Hello, I must apologise, I only glanced at your data and misinterpreted what I saw, I should have been more careful.

Can you say what calculation you wish to perform on this example dataframe ?

nirgrahamuk

I want to avoid this error:

summarise() has grouped output by 'ANO_REF'. You can override using the .groups argument.

which I've described in the first post.

On my second post I wrote the full command.

Thank you.

ok here is the documentation on the different options.

.groups	Experimental lifecycle
Grouping structure of the result.

"drop_last": dropping the last level of grouping. This was the only supported option before version 1.0.0.

"drop": All levels of grouping are dropped.

"keep": Same grouping structure as .data.

"rowwise": Each row is it's own group.

When .groups is not specified, it is chosen based on the number of rows of the results:

If all the results have 1 row, you get "drop_last".

If the number of rows varies, you get "keep".

In addition, a message informs you of that choice, unless the option "dplyr.summarise.inform" is set to FALSE, or when summarise() is called from a function in a package.#

You should think about , what grouping do I want set on my data.frame after the summarise function has completed. if the answer is none then you can "drop", if you want the same that was set before the summarise "keep" etc.

 summarise(VF = sum(VF), .groups = "drop")

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.

What I'm trying to do is this:

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)}
      
    }
  }
}