knitr::opts_chunk$set( collapse = TRUE, comment = "#>" )
This is a collection of short scripts (recipes) to do some common things using brandseyer2. You should, for the most part, be able to copy and paste them in to an R file, change a few variables (like the account code and filter), and then run them.
Sometimes you may want to get a clean data set of mentions, and the topics on them.
This small script will write out a CSV giving, for each topic on a mention, a row with the mention
ID, topic ID, and topic name. It reads all the mentions matching a filter, asking
for only the mentions' IDs and tags. Then, using topics()
, it gets all the topic
data for the mentions. Next, it uses dplyr::select()
to choose only some interesting
columns to keep. Finaly, it saves the data, using readr::write_csv()
.
library(brandseyer2) library(dplyr, warn.conflicts = FALSE) # For manipulating tables library(readr) # For writing CSVs code <- "BEUB03AA" filter <- "published inthelast week and brand isorchildof 46334" mentions_with_topics <- account(code) %>% # Get the account mentions(filter, select = c(id, tags)) %>% # Ask for the mentions (but only their IDs and tags) topics() %>% # From the mentions, get specific topics select(id, topic.id, name) # Select only some interesting columns write_csv(mentions_with_topics, "topics.csv")
You can modify what columns you'd like kept in your CSV file by modifying the select
line.
And this is what some of the data looks like:
#> # A tibble: 2,475 x 3 #> id topic.id name #> <chr> <int> <chr> #> 1 46334-8933… 70331 Comparing (brands to brands) or comparing (brands… #> 2 46334-8933… 69873 Ethics or reputation #> 3 46334-8933… 71303 Transport experience #> 4 46334-8933… 71359 Drivers #> 5 46334-8933… 69878 Accusations of unethical behavior #> 6 46334-8933… 69873 Ethics or reputation #> 7 46334-8933… 70331 Comparing (brands to brands) or comparing (brands… #> 8 46334-8933… 69873 Ethics or reputation #> 9 46334-8933… 70331 Comparing (brands to brands) or comparing (brands… #> 10 46334-8933… 69873 Ethics or reputation #> # ... with 2,465 more rows
This is an extension of the recipe for finding topics on a mention. It uses dplyr to calculate the average number of mentions on a topic.
library(brandseyer2) library(dplyr) library(readr) code <- "BEUB03AA" filter <- "published inthelast week and brand isorchildof 46334" mentions_with_topics <- account(code) %>% # Get the account mentions(filter, select = c(id, tags)) %>% # Ask for the mentions (but only their IDs and tags) topics() %>% # From the mentions, get specific topics select(id, topic.id, name) topics_per_mention <- mentions_with_topics %>% group_by(id) %>% # For each mention summarise(count = n()) # Count the number of mentions average_topics <- sum(topics_per_mention$count) / nrow(topics_per_mention) message(glue::glue("The average number of topics per mention is {round(average_topics, 2)}"))
The recipe prints out the average
#> The average number of topics per mention is 2.84
BrandsEye topics can belong to more than one topic tree, and so can have more
than one parent. When wanting to find the parent of a topic, you need to choose
a topic tree to do this from. The easiest way to find topic trees is to use the
topic_trees()
function, like so:
library(brandseyer2) library(dplyr) account("TEST01AA") %>% topic_trees() %>% select(id, name)
library(brandseyer2) library(dplyr) account("TEST01AA") %>% tags() %>% with_tag_parents(1001) %>% filter(namespace == 'topic') %>% arrange(desc(is_parent), name)
This produces a nicely sorted table of topics and parents, all in relation to the given topic tree. Notice that the topic tree itself won't be in this table, since it is in its own topic_tree namespace.
library(brandseyer2) library(dplyr) account("TEST01AA") %>% tags() %>% with_tag_parents(1001) %>% filter(namespace == 'topic') %>% arrange(desc(is_parent), name) %>% select(id, name, parent)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.