Skip to contents

Here, we are not counting the number of rows in the dataset, but rather we are counting the number observations for each keys in the data.

Usage

add_n_obs(.data, ...)

Arguments

.data

tsibble

...

extra arguments

Value

tsibble with n_obs, the number of observations per key added.

Examples

library(dplyr)
#> 
#> Attaching package: ‘dplyr’
#> The following objects are masked from ‘package:stats’:
#> 
#>     filter, lag
#> The following objects are masked from ‘package:base’:
#> 
#>     intersect, setdiff, setequal, union
# you can explore the data to see those cases that have exactly two 
 # observations:
heights %>% 
  add_n_obs() %>% 
  filter(n_obs == 2)
#> # A tsibble: 16 x 5 [!]
#> # Key:       country [8]
#>    country              year n_obs continent height_cm
#>    <chr>               <dbl> <int> <chr>         <dbl>
#>  1 Botswana             1910     2 Africa         165.
#>  2 Botswana             1980     2 Africa         167.
#>  3 Burundi              1920     2 Africa         166.
#>  4 Burundi              1930     2 Africa         169.
#>  5 Costa Rica           1940     2 Americas       166.
#>  6 Costa Rica           1980     2 Americas       174.
#>  7 El Salvador          1990     2 Americas       169.
#>  8 El Salvador          2000     2 Americas       171.
#>  9 Libya                1890     2 Africa         166.
#> 10 Libya                1920     2 Africa         165.
#> 11 Mongolia             1910     2 Asia           163.
#> 12 Mongolia             1930     2 Asia           165.
#> 13 Singapore            1970     2 Asia           172.
#> 14 Singapore            2000     2 Asia           175.
#> 15 Trinidad and Tobago  1980     2 Americas       174.
#> 16 Trinidad and Tobago  2000     2 Americas       174.