Skip to contents

is_first() is TRUE for the first row of each distinct combination of its arguments and FALSE for the others, like Stata's egen t = tag(id year). Use it in dplyr::mutate() or dplyr::filter(), for example to count or summarise each group once.

Usage

is_first(..., .missing = FALSE)

Arguments

...

Vectors of equal length that define the groups. Omit them in grouped dplyr::mutate() (or with .by) to flag the first row of each group.

.missing

If FALSE (default), rows with a missing value in any argument are FALSE, as Stata's tag() does without its missing option. If TRUE, missing values form groups of their own.

Value

A logical vector with one value per input row, or per row in the current data-mask group when no arguments are supplied.

Details

"First" follows the current row order. Unlike duplicates_tag(), which counts the other rows sharing the same values, is_first() marks one row per group. Missing values include tagged and SPSS user-missing values.

See also

Other data checking functions: assert(), keys, stata_merge()

Examples

panel <- tibble::tibble(
  id = c(1, 1, 2, 2, 2, NA),
  year = c(2001, 2002, 2001, 2002, 2003, 2001)
)
dplyr::mutate(panel, first = is_first(id))
#> # A tibble: 6 x 3
#>      id  year first
#>   <dbl> <dbl> <lgl>
#> 1     1  2001 TRUE 
#> 2     1  2002 FALSE
#> 3     2  2001 TRUE 
#> 4     2  2002 FALSE
#> 5     2  2003 FALSE
#> 6    NA  2001 FALSE

# Including the missing group
dplyr::mutate(panel, first = is_first(id, .missing = TRUE))
#> # A tibble: 6 x 3
#>      id  year first
#>   <dbl> <dbl> <lgl>
#> 1     1  2001 TRUE 
#> 2     1  2002 FALSE
#> 3     2  2001 TRUE 
#> 4     2  2002 FALSE
#> 5     2  2003 FALSE
#> 6    NA  2001 TRUE 

# An alternative
dplyr::mutate(panel, first = is_first(), .by = id)
#> # A tibble: 6 x 3
#>      id  year first
#>   <dbl> <dbl> <lgl>
#> 1     1  2001 TRUE 
#> 2     1  2002 FALSE
#> 3     2  2001 TRUE 
#> 4     2  2002 FALSE
#> 5     2  2003 FALSE
#> 6    NA  2001 TRUE 

# Count panels
is_first(panel$id) |> sum()
#> [1] 2