0

I have this df

 ZONA   ID_DOM   FE_DOM NO_MORAD
1    1 00010001 15.41667        2
2    1 00010001 15.41667        2
3    1 00010001 15.41667        2
4    1 00010001 15.41667        2
5    1 00010001 15.41667        2
6    1 00010002 15.41667        4
...
    99 00010994 16.68444        5 

Currently, I'm counting the number of ID_DOM using the weight variable FE_DOM with this code.

count(OD_2017[!duplicated(OD_2017$ID_DOM),], 
      wt = FE_DOM, 
      Zonas = ZONA,
      name = "N_domicilios")

which returns me

  Zonas N_domicilios
  <int>        <dbl>
1     1        1151.
2     2        2342.
3     3        7100.
4     4       12588.
5     5        8050.
6     6        9411.

However I want this data grouped by NO_MORAD, something like

Zonas 1Mor 2Mor ... 99Mor 
1     50   78   ... 78
2     x    y    ... z
...
99    99   99   ... 99

Can anyone help me with this? Thanks

  • It isn't clear how one might get from your sample data to your expected output. Perhaps `xtabs(~ Zonas + NO_MORAD, data=df)`? – r2evans Mar 24 '20 at 22:02
  • but your answer doesn't get me the wheighted count. – Aquiles Silva Mar 24 '20 at 22:40
  • 2
    Please make this question *reproducible*. This includes sample code attempted (including listing non-base R packages), sample *unambiguous* data (e.g., `dput(head(x))` or `data.frame(x=...,y=...)`), and expected output. Refs: https://stackoverflow.com/questions/5963269, [mcve], and https://stackoverflow.com/tags/r/info. – r2evans Mar 24 '20 at 22:41
  • I tried to improve my question, but couldn't get where is the problem from my sample (head) to my expected output. My df has maaaany rows and a lot of different zones, IDs, weights and so on. The columns in the expected output are the different numbers which i can get with smt like unique(NO_MORAD). better now, maybe? – Aquiles Silva Mar 24 '20 at 22:42
  • 2
    Please read my comment and any of the links I provided. Most of them suggest using `dput(head(x))` (where x is your frame) and pasting that output into the question. Note about "representative" sample, though, is that it would be nice to (a) show enough data so that we have 2-3 different grouping variables (`Zonas` and `NO_MORAD`), and then (b) provide expected output *given the sample data you provided* (that is actually hand-calculated). – r2evans Mar 25 '20 at 01:22
  • @AquilesSilva Hi, your data really sounds too complicated to provide an excerpt. Have you read https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610#5963610 ? In most cases it is much better to provide toy data as described there. All that is needed for us to answer your question is a minimal, self-contained, reproducible example as _r2evans_ already suggested well. – jay.sf Mar 26 '20 at 08:02

0 Answers0