2

I am trying to learn dplyr and I was using nywflights13 package. I was trying to find out proportions of flights from Origin(NYC(EWR,JFK)) to destination(Seattle)

The output I am expecting is,

Origin   n     Prop
JFK     2092     53
EWR     1831     47

I am able to do it in other ways. But I am trying to find using dplyr package. I tried the following without much success,

 library("nycflights13")

   flights %>% filter(dest=="SEA") %>% group_by(origin) %>% 
summarise(n=n(origin)) %>% mutate(Prop= n / sum(n))

But I am getting an error as Error in n(origin) : unused argument (origin)

Can anybody help me what correction I should do in this?

haimen
  • 1,985
  • 7
  • 30
  • 53

1 Answers1

2

If both plyr and dplyr are loaded, we could have masking of summarise, mutate etc. It is better to use

  dplyr::summarise(n=n())

Using the full code,

  flights %>% 
    filter(dest=="SEA") %>%
    group_by(origin) %>% 
    dplyr::summarise (n = n()) %>%
    dplyr::mutate(freq = n / sum(n))
 # Source: local data frame [2 x 3]

 #  origin     n      freq
 #  (chr) (int)     (dbl)
 #1    EWR  1831 0.4667346
 #2    JFK  2092 0.5332654
akrun
  • 874,273
  • 37
  • 540
  • 662
  • flights %>% filter(dest=="SEA") %>% group_by(origin) %>% summarise (n = n()) %>% mutate(freq = n / sum(n)) When i use this, I get the following error, Error in n() : This function should not be called directly – haimen Oct 19 '15 at 05:38
  • Try `dplyr::summarise(n=n())` – akrun Oct 19 '15 at 05:38
  • where should I use this? – haimen Oct 19 '15 at 05:39
  • @haimen This works for me. As I mentioned, it must be a case of masking function by plyr::summarise if both libraries are loaded. – akrun Oct 19 '15 at 05:40
  • 1
    Yeah It works for me too.. not able to accept the answer now. Will do it in `10 mins. – haimen Oct 19 '15 at 05:41
  • I was just wondering what was the reason I was missing it.. Thanks a lot – haimen Oct 19 '15 at 05:42
  • @haimen Check [here](http://stackoverflow.com/questions/26106146/why-does-summarise-on-grouped-data-result-in-only-overall-summary-in-dplyr) – akrun Oct 19 '15 at 05:47