Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does n=n( ) mean in R?

Tags:

r

dplyr

grouping

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())
like image 272
Glassjawed Avatar asked Sep 16 '14 12:09

Glassjawed


1 Answers

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.

like image 195
John Paul Avatar answered Oct 26 '22 12:10

John Paul