ComeçarComece de graça

Extracting mentions

In each sublist of the dataset of tweets, there is an element called "mentions_screen_name" (i.e. Twitter handles). This element contains either NULL if there was no mention in the tweet, or one or more screen names mentioned in the tweet. A way to detect a popular account from a list of tweets is to detect who are the most mentioned users in a specific tweet collection.

We'll first extract a vector of all mentions, and once we've got this new vector, we'll count the number of time each profile is mentioned. To do that, we'll build a new composed function, by combining table() (which counts the number of occurrences of each element in the vector), sort(), and tail().

purrr has been loaded for you, and rstudioconf is available in your dataset.

Este exercício faz parte do curso

Intermediate Functional Programming with purrr

Ver curso

Instruções do exercício

  • Build a function that is the combination of as_vector(), compact(), and flatten().

  • Create a function that takes two arguments: list and what. This function will run map( list, what ), and pass the result to flatten_to_vector.

  • Create six_most, a function that combines tail(), sort(), and table().

  • Run extractor() on rstudioconf, and pass the result to six_most().

Exercício interativo prático

Experimente este exercício completando este código de exemplo.

# Combine as_vector(), compact(), and flatten()
flatten_to_vector <- ___(___, ___, ___)

# Complete the function
extractor <- function(list, what = "mentions_screen_name"){
  map( ___ , ___ ) %>%
    ___()
}

# Create six_most, with tail(), sort(), and table()
six_most <- ___(___, ___, ___)

# Run extractor() on rstudioconf
___(rstudioconf) %>% 
  ___()
Editar e executar o código