Returns the number of observations of a vector or data.frame. It uses vctrs::vec_size() under the hood.

n_obs(x, names = TRUE)

Arguments

x

vector or data.frame

names

logical; If TRUE the result is a named vector named "n_obs", else it is just the number of observations.

Value

number of observations

Note

You cannot use n_obs with features counting the key variable like so - features(wages, id, n_obs). Instead, use any other variable.

Examples

n_obs(iris)
#> n_obs #> 150
n_obs(1:10)
#> n_obs #> 10
add_n_obs(wages)
#> # A tsibble: 6,402 x 10 [!] #> # Key: id [888] #> id xp n_obs ln_wages ged xp_since_ged black hispanic high_grade #> <int> <dbl> <int> <dbl> <int> <dbl> <int> <int> <int> #> 1 31 0.015 8 1.49 1 0.015 0 1 8 #> 2 31 0.715 8 1.43 1 0.715 0 1 8 #> 3 31 1.73 8 1.47 1 1.73 0 1 8 #> 4 31 2.77 8 1.75 1 2.77 0 1 8 #> 5 31 3.93 8 1.93 1 3.93 0 1 8 #> 6 31 4.95 8 1.71 1 4.95 0 1 8 #> 7 31 5.96 8 2.09 1 5.96 0 1 8 #> 8 31 6.98 8 2.13 1 6.98 0 1 8 #> 9 36 0.315 10 1.98 1 0.315 0 0 9 #> 10 36 0.983 10 1.80 1 0.983 0 0 9 #> # … with 6,392 more rows, and 1 more variable: unemploy_rate <dbl>
wages %>% features(ln_wages, n_obs) # can be any variable except id, the key.
#> # A tibble: 888 x 2 #> id n_obs #> <int> <int> #> 1 31 8 #> 2 36 10 #> 3 53 8 #> 4 122 10 #> 5 134 12 #> 6 145 9 #> 7 155 11 #> 8 173 6 #> 9 206 3 #> 10 207 11 #> # … with 878 more rows