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(heights, country, n_obs). Instead, use any other variable.

Examples

n_obs(iris)
#> n_obs #> 150
n_obs(1:10)
#> n_obs #> 10
add_n_obs(heights)
#> # A tsibble: 1,490 x 5 [!] #> # Key: country [144] #> country year n_obs continent height_cm #> <chr> <dbl> <int> <chr> <dbl> #> 1 Afghanistan 1870 5 Asia 168. #> 2 Afghanistan 1880 5 Asia 166. #> 3 Afghanistan 1930 5 Asia 167. #> 4 Afghanistan 1990 5 Asia 167. #> 5 Afghanistan 2000 5 Asia 161. #> 6 Albania 1880 4 Europe 170. #> 7 Albania 1890 4 Europe 170. #> 8 Albania 1900 4 Europe 169. #> 9 Albania 2000 4 Europe 168. #> 10 Algeria 1910 5 Africa 169. #> # … with 1,480 more rows
heights %>% features(height_cm, n_obs) # can be any variable except id, the key.
#> # A tibble: 144 x 2 #> country n_obs #> <chr> <int> #> 1 Afghanistan 5 #> 2 Albania 4 #> 3 Algeria 5 #> 4 Angola 9 #> 5 Argentina 20 #> 6 Armenia 11 #> 7 Australia 10 #> 8 Austria 18 #> 9 Azerbaijan 7 #> 10 Bahrain 3 #> # … with 134 more rows