-1

With a data frame df1 like below

+-----------------------------------------+
|reg   |make  |model  |year|abs  |gears|fm|
+-----------------------------------------+
|ax1234|Toyota|Corolla|1999|true |6    |0 |
|ax1235|Toyota|Corolla|1999|false|5    |0 |
|ax1236|Toyota|Corolla|1992|false|4    |NA|
|ax1237|Toyota|Camry  |2001|true |7    |1 |
|ax1238|Honda |Civic  |1994|true |5    |NA|
|ax1239|Honda |Civic  |2000|false|6    |0 |
|ax1240|Honda |Accord |1992|false|4    |NA|
|ax1241|Nissan|Sunny  |2001|true |6    |0 |
|ax1242|      |       |1998|false|6    |0 |
|ax1243|NA    |NA     |1992|false|4    |NA|
+-----------------------------------------+

On aggregation like below, I want to preserve makes with NA - how to achieve this ? It is fine to have the make and NA are combined together.

> aggregate(reg ~ make, df1, length)
    make reg
1          1
2  Honda   3
3 Nissan   1
4 Toyota   4
user3206440
  • 4,749
  • 15
  • 75
  • 132
  • 1
    `aggregate(reg ~ addNA(make), df1, length)` from the duplicate. For me it worked. – jogo Jan 30 '17 at 08:37

1 Answers1

2

We can use dplyr and it gives the NA count as well

library(dplyr)
 df1 %>% 
    group_by(make) %>%
    summarise(reg = n())
akrun
  • 874,273
  • 37
  • 540
  • 662