dplyr::n()
   get_help() docs


Description

The n() function is part of the {dplyr} package, which is part of the {tidyverse}.

The function n() is a helper function that returns a number representing the total number of rows in a given grouping. It is most useful if you want to add an ‘index column’ in a tibble (data frame).

To use this function, you need to either first load the {dplyr} library, or always use the function with dplyr::n() notation.

# Load the library
library(dplyr)
# Or, load the full tidyverse:
library(tidyverse)

# Or, use :: notation
dplyr::n()

Conceptual Usage

tibble %>% 
  mutate(column_ranging_1_to_n = 1:n())

Examples

The examples below use the carnivores dataset. Learn more about this dataset with get_help("carnivores").

# Show the carnivores dataset
carnivores
## # A tibble: 9 × 4
##   name              genus        awake brainwt
##   <chr>             <fct>        <dbl>   <dbl>
## 1 Arctic fox        Vulpes        11.5  0.0445
## 2 Cheetah           Acinonyx      11.9 NA     
## 3 Dog               Canis         13.9  0.07  
## 4 Gray seal         Haliochoerus  17.8  0.325 
## 5 Jaguar            Panthera      13.6  0.157 
## 6 Lion              Panthera      10.5 NA     
## 7 Northern fur seal Callorhinus   15.3 NA     
## 8 Red fox           Vulpes        14.2  0.0504
## 9 Tiger             Panthera       8.2 NA


# Make a new column `one_to_n` that counts the rows 1-n in carnivores
carnivores %>%
  mutate(one_to_n = 1:n())
## # A tibble: 9 × 5
##   name              genus        awake brainwt one_to_n
##   <chr>             <fct>        <dbl>   <dbl>    <int>
## 1 Arctic fox        Vulpes        11.5  0.0445        1
## 2 Cheetah           Acinonyx      11.9 NA             2
## 3 Dog               Canis         13.9  0.07          3
## 4 Gray seal         Haliochoerus  17.8  0.325         4
## 5 Jaguar            Panthera      13.6  0.157         5
## 6 Lion              Panthera      10.5 NA             6
## 7 Northern fur seal Callorhinus   15.3 NA             7
## 8 Red fox           Vulpes        14.2  0.0504        8
## 9 Tiger             Panthera       8.2 NA             9


# Make a new column `one_to_n` that counts the rows in carnivores, but
# where the numbering restarts for each `genus` grouping
carnivores %>%
  group_by(genus) %>%
  mutate(one_to_n = 1:n()) %>%
  # arranging by genus to make the output easier to see
  arrange(genus)
## # A tibble: 9 × 5
## # Groups:   genus [6]
##   name              genus        awake brainwt one_to_n
##   <chr>             <fct>        <dbl>   <dbl>    <int>
## 1 Cheetah           Acinonyx      11.9 NA             1
## 2 Northern fur seal Callorhinus   15.3 NA             1
## 3 Dog               Canis         13.9  0.07          1
## 4 Gray seal         Haliochoerus  17.8  0.325         1
## 5 Jaguar            Panthera      13.6  0.157         1
## 6 Lion              Panthera      10.5 NA             2
## 7 Tiger             Panthera       8.2 NA             3
## 8 Arctic fox        Vulpes        11.5  0.0445        1
## 9 Red fox           Vulpes        14.2  0.0504        2