Skip to contents

These functions are inspired by dplyr::count() and dplyr::tally(). spq_tally() assumes you've already done the grouping.

Usage

spq_tally(.query, sort = FALSE, name = "n")

spq_count(.query, ..., sort = FALSE, name = "n")

Arguments

.query

a list with elements of the query

sort

If TRUE, will show the largest groups at the top. (like the sort argument of dplyr::tally())

name

Name for the count column (like the name argument of dplyr::tally())

...

variables by which to arrange (or SPARQL strings escaped with spq(), or strings, see examples)

Value

A query object

Examples

# \dontrun{
spq_init() %>%
spq_add("?film wdt:P31 wd:Q11424") %>%
spq_mutate(narrative_location = wdt::P840(film)) %>%
spq_label(narrative_location) %>%
spq_tally(name = "n_films") %>%
spq_perform()
#> # A tibble: 1 × 1
#>   n_films
#>     <int>
#> 1   51290

# the same with spq_count

spq_init() %>%
spq_add("?film wdt:P31 wd:Q11424") %>%
spq_mutate(narrative_location = wdt::P840(film)) %>%
spq_label(narrative_location) %>%
spq_count(name = "n_films") %>%
spq_perform()
#> # A tibble: 1 × 1
#>   n_films
#>     <int>
#> 1   51290

# Now with grouping
spq_init() %>%
spq_add("?film wdt:P31 wd:Q11424") %>%
spq_mutate(narrative_location = wdt::P840(film)) %>%
spq_label(film, narrative_location) %>%
spq_group_by(narrative_location_label) %>%
spq_tally(sort = TRUE, name = "n_films") %>%
spq_perform()
#> # A tibble: 2,796 × 2
#>    narrative_location_label n_films
#>    <chr>                      <int>
#>  1 Indonesia                   3089
#>  2 New York City               2547
#>  3 London                      2223
#>  4 Paris                       1854
#>  5 Los Angeles                 1495
#>  6 England                     1427
#>  7 Rome                         969
#>  8 California                   839
#>  9 France                       795
#> 10 Hong Kong                    706
#> # ℹ 2,786 more rows


# More direct with spq_count()
spq_init() %>%
spq_add("?film wdt:P31 wd:Q11424") %>%
spq_mutate(narrative_location = wdt::P840(film)) %>%
spq_label(film, narrative_location) %>%
spq_count(narrative_location_label, sort = TRUE, name = "n_films") %>%
spq_perform()
#> # A tibble: 2,796 × 2
#>    narrative_location_label n_films
#>    <chr>                      <int>
#>  1 Indonesia                   3089
#>  2 New York City               2547
#>  3 London                      2223
#>  4 Paris                       1854
#>  5 Los Angeles                 1495
#>  6 England                     1427
#>  7 Rome                         969
#>  8 California                   839
#>  9 France                       795
#> 10 Hong Kong                    706
#> # ℹ 2,786 more rows
# }