12

The other day I was reading the following lines in R and I don't understand what the %>% and summarise(n=n()) and summarise(total=n()) meant. I understand the group_by and ungroup methods though.

Can someone help out? There isn't any documentation for this either.

library(dplyr)
net.multiplicity <- group_by(net, nodeid, epoch) %>% summarise(n=n()) %>%
                    ungroup() %>% group_by(n) %>% summarise(total=n())
David Arenburg
  • 91,361
  • 17
  • 137
  • 196
Kashif
  • 3,063
  • 6
  • 29
  • 45
  • 3
    There is documentation for `n()`. Type `?n()` in console. It is basically the number of observations within a group – David Arenburg Sep 16 '14 at 13:01
  • `%>%` is a newish form of syntax to allow pipes and, loosely, makes code readable/writable from left-to-right. See [this summary](http://www.r-statistics.com/2014/08/simpler-r-coding-with-pipes-the-present-and-future-of-the-magrittr-package/) for more details. – Dan Sep 16 '14 at 13:03
  • Threre is also documentation for `%>%`. Type `?"%>%"` (you need the quotes because of the special characters) – nico Sep 16 '14 at 13:03
  • If you are going to do `?n()` or `?"%>%"` make sure you have `dplyr` loaded first i.e. `library(dplyr)` or it won't work. – John Paul Sep 16 '14 at 13:08

1 Answers1

17

This is from the dplyr package. n=n() means that a variable named n will be assigned the number of rows (think number of observations) in the summarized data.

the %>% is read as "and then" and is way of listing your functions sequentially rather then nesting them. So that command is saying you should do the grouping and then summarize the result of the grouping by the number of rows in each group and then ungroup that result, and then group the un-grouped data based on n and then summarize that by the total number of rows in each of the new groups.

John Paul
  • 12,196
  • 6
  • 55
  • 75
  • Would we even need to ungroup? We could just group the grouped data based on n instead of grouping ungrouped data, couldn't we? – Kashif Sep 16 '14 at 13:15
  • @Glassjawed I think you are correct. You can use an additional argument `add` that by default is `FALSE`, but when `TRUE` it adds to the exiting groups rather than overriding them. – John Paul Sep 16 '14 at 13:18