### Install quanteda.tidy Source: https://context7.com/quanteda/quanteda.tidy/llms.txt Installs the stable version from CRAN or the development version from GitHub. Loads the library for use. ```r install.packages("quanteda.tidy") ``` ```r pak::pkg_install("quanteda/quanteda.tidy") ``` ```r library(quanteda.tidy) ``` -------------------------------- ### Install quanteda.tidy from CRAN Source: https://github.com/quanteda/quanteda.tidy/blob/master/README.md Installs the stable version of the quanteda.tidy package from the Comprehensive R Archive Network (CRAN). ```r install.packages("quanteda.tidy") ``` -------------------------------- ### Install quanteda.tidy Development Version from GitHub Source: https://github.com/quanteda/quanteda.tidy/blob/master/README.md Installs the latest development version of the quanteda.tidy package directly from its GitHub repository using the pak package manager. ```r pak::pkg_install("quanteda/quanteda.tidy") ``` -------------------------------- ### Subset documents by position using slice() Source: https://context7.com/quanteda/quanteda.tidy/llms.txt Selects documents by integer index. slice_head() and slice_tail() select from the start or end, accepting n (count) or prop (proportion). ```r # Select specific positions slice(data_corpus_inaugural, 1:3) |> summary() # First 10 % of the corpus slice_head(data_corpus_inaugural, prop = 0.10) |> summary() # Last 3 documents slice_tail(data_corpus_inaugural, n = 3) |> summary() ## Corpus consisting of 3 documents, showing 3 documents: ## ## Text Types Tokens Sentences Year President FirstName Party ## 2009-Obama ... ## 2013-Obama ... ## 2017-Trump ... ``` -------------------------------- ### Compact overview of corpus docvars with glimpse() Source: https://context7.com/quanteda/quanteda.tidy/llms.txt Prints a transposed, console-width-aware summary of all docvars with truncated text previews. This is the corpus equivalent of `tibble::glimpse()`. ```r glimpse(data_corpus_inaugural) ``` -------------------------------- ### Randomly sample documents using slice_sample() Source: https://context7.com/quanteda/quanteda.tidy/llms.txt Randomly selects n documents or a prop fraction. Supports sampling with replacement and probability weights. ```r set.seed(42) # Draw 5 random speeches slice_sample(data_corpus_inaugural, n = 5) |> summary() # Sample 10 % with replacement (bootstrapping) slice_sample(data_corpus_inaugural, prop = 0.10, replace = TRUE) |> ndoc() ## [1] 6 ``` -------------------------------- ### Reorder document variable columns with relocate() Source: https://context7.com/quanteda/quanteda.tidy/llms.txt Moves specified docvars to the front (default) or before/after a reference column using `.before` or `.after`. Useful for organizing columns for analysis or display. ```r # Move Party and President to the front data_corpus_inaugural | relocate(Party, President) | summary(n = 3) ``` ```r # Place FirstName and President immediately before Year data_corpus_inaugural | relocate(FirstName, President, .before = Year) | summary(n = 3) ``` -------------------------------- ### Create or modify document variables with mutate() / transmute() Source: https://context7.com/quanteda/quanteda.tidy/llms.txt Use `mutate()` to add new docvars or overwrite existing ones, preserving all others. `transmute()` adds new variables but drops all docvars not explicitly named. ```r # Add two derived docvars data_corpus_inaugural | mutate( fullname = paste(FirstName, President), century = floor(Year / 100) + 1, n_tokens = ntoken(data_corpus_inaugural) ) | summary(n = 5) ``` ```r # Keep only the derived variables (drops Year, President, FirstName, Party) data_corpus_inaugural | transmute( speech_id = paste(Year, President, sep = "-"), party = Party ) | summary(n = 5) ``` -------------------------------- ### Rename document variables with rename() / rename_with() Source: https://context7.com/quanteda/quanteda.tidy/llms.txt Use `rename()` for direct renaming with `new_name = old_name` syntax. Use `rename_with()` to apply a function to selected column names, such as `toupper` or `tolower`. ```r # Direct rename data_corpus_inaugural | rename(LastName = President, GivenName = FirstName) | summary(n = 3) ``` ```r # Uppercase all docvar names data_corpus_inaugural | rename_with(toupper) | names() ``` ```r # Uppercase only names starting with "P" data_corpus_inaugural | rename_with(toupper, starts_with("P")) | names() ``` -------------------------------- ### Add Full Name Document Variable using mutate Source: https://github.com/quanteda/quanteda.tidy/blob/master/README.md Demonstrates how to add a new document variable 'fullname' to a quanteda corpus by combining 'FirstName' and 'President' using the mutate function. The summary function is then used to display the first few documents with the new variable. ```r library("quanteda.tidy", warn.conflicts = FALSE) ## Loading required package: quanteda ## Package version: 4.3.1 ## Unicode version: 14.0 ## ICU version: 71.1 ## Parallel computing: disabled ## See https://quanteda.io for tutorials and examples. data_corpus_inaugural %>% mutate(fullname = paste(FirstName, President, sep = ", ")) %>% summary(n = 5) ## Corpus consisting of 60 documents, showing 5 documents: ## ## Text Types Tokens Sentences Year President FirstName ## 1789-Washington 625 1537 23 1789 Washington George ## 1793-Washington 96 147 4 1793 Washington George ## 1797-Adams 826 2577 37 1797 Adams John ## 1801-Jefferson 717 1923 41 1801 Jefferson Thomas ## 1805-Jefferson 804 2380 45 1805 Jefferson Thomas ## Party fullname ## none George, Washington ## none George, Washington ## Federalist John, Adams ## Democratic-Republican Thomas, Jefferson ## Democratic-Republican Thomas, Jefferson ``` -------------------------------- ### Keep unique documents by docvar combinations using distinct() Source: https://context7.com/quanteda/quanteda.tidy/llms.txt Retains only the first document for each unique combination of specified document variables. Use .keep_all = TRUE to retain all other docvars. ```r # One speech per president (first occurrence) data_corpus_inaugural %>% distinct(President, .keep_all = TRUE) %>% ndoc() ## [1] 42 (42 unique presidents) # Unique Party–President pairs, keeping all docvars data_corpus_inaugural %>% distinct(Party, President, .keep_all = TRUE) %>% summary(n = 8) ``` -------------------------------- ### Join on document names using 'docname' key Source: https://context7.com/quanteda/quanteda.tidy/llms.txt Join corpus metadata with external data frames using the special 'docname' key for document names. This is useful for adding specific notes or metadata to individual documents. ```R doc_metadata <- data.frame( docname = c("1789-Washington", "1793-Washington", "1797-Adams"), notes = c("First inaugural", "Second inaugural", "First Adams speech") ) data_corpus_inaugural[1:5] | left_join(doc_metadata, by = "docname") | summary() ``` -------------------------------- ### Add group-count docvars with add_count() / add_tally() Source: https://context7.com/quanteda/quanteda.tidy/llms.txt Use `add_count()` to group by docvars and append the per-group document count. Use `add_tally()` to append the total document count without grouping. ```r # Count speeches per president; filter to multi-term presidents data_corpus_inaugural | add_count(President, name = "n_speeches") | filter(n_speeches > 1) | distinct(President, n_speeches, .keep_all = FALSE) | arrange(desc(n_speeches)) ``` ```r # Count within Party AND President data_corpus_inaugural | add_count(Party, President, name = "party_pres_n") | summary(n = 5) ``` ```r # Add total corpus size as a docvar data_corpus_inaugural | slice(1:5) | add_tally(name = "total_docs") | pull(total_docs) ``` -------------------------------- ### Reorder documents by docvar values using arrange() Source: https://context7.com/quanteda/quanteda.tidy/llms.txt Sorts corpus documents by one or more document variables. Use dplyr::desc() to reverse the sort order. ```r # Alphabetical order by president last name data_corpus_inaugural[1:10] %>% arrange(President) %>% summary() # Most recent speeches first data_corpus_inaugural %>% arrange(desc(Year)) %>% slice_head(n = 5) %>% pull(Year) ## [1] 2017 2013 2009 2005 2001 ``` -------------------------------- ### Merge a data frame into a corpus with left_join() Source: https://context7.com/quanteda/quanteda.tidy/llms.txt Adds columns from an external data frame `y` to corpus `x` by matching on shared docvars. Unmatched documents receive `NA` for the new columns. Supports joining on document names using the special key `"docname"`. ```r ``` -------------------------------- ### Join on a regular docvar with left_join Source: https://context7.com/quanteda/quanteda.tidy/llms.txt Use left_join to combine corpus metadata with external data frames based on a common document variable (docvar). Ensure the 'Party' column exists in both the corpus and the data frame. ```R party_info <- data.frame( Party = c("Democratic", "Republican", "none", "Federalist", "Democratic-Republican", "Whig"), hex_color = c("#3333FF", "#FF3333", "#999999", "#800080", "#009900", "#FF8C00") ) data_corpus_inaugural | left_join(party_info, by = "Party") | select(President, Party, hex_color) | summary(n = 6) ``` -------------------------------- ### Join using differently-named columns with left_join Source: https://context7.com/quanteda/quanteda.tidy/llms.txt When document identifiers have different names in the corpus and the external data frame, use a named vector in the 'by' argument of left_join to specify the mapping (e.g., by = c("docname" = "doc_id")). ```R ratings <- data.frame( doc_id = c("1789-Washington", "2009-Obama"), rating = c(5L, 5L) ) data_corpus_inaugural | left_join(ratings, by = c("docname" = "doc_id")) | filter(!is.na(rating)) | pull(President) ``` -------------------------------- ### Select documents with extreme docvar values Source: https://context7.com/quanteda/quanteda.tidy/llms.txt Selects the n documents with the lowest (slice_min) or highest (slice_max) value of a specified document variable. Requires token counts to be added via mutate() first. ```r # Add token counts first via mutate(), then slice corp <- data_corpus_inaugural %>% mutate(n_tokens = ntoken(data_corpus_inaugural)) # Three shortest inaugural addresses slice_min(corp, n_tokens, n = 3) |> summary() ## ...1793-Washington 147 tokens... # Three longest inaugural addresses slice_max(corp, n_tokens, n = 3) |> summary() ## ...1841-Harrison 9125 tokens... ``` -------------------------------- ### Keep or drop document variables with select() Source: https://context7.com/quanteda/quanteda.tidy/llms.txt Retains or removes docvars using tidyselect helpers. Use for filtering specific variables based on names or patterns. ```r # Keep only President and Year docvars data_corpus_inaugural | select(President, Year) | summary(n = 5) ``` ```r # Drop variables whose name starts with "First" data_corpus_inaugural | select(-starts_with("First")) | summary(n = 5) ``` ```r # Keep all variables between Year and Party (inclusive) data_corpus_inaugural | select(Year:Party) | names() |> head() ``` -------------------------------- ### Filter documents by docvar conditions Source: https://context7.com/quanteda/quanteda.tidy/llms.txt Keeps documents where logical predicates on document variables are TRUE. Equivalent to quanteda::corpus_subset() but uses dplyr syntax. Documents with NA conditions are dropped. ```r library(quanteda.tidy) # Keep only 20th-century Democratic speeches data_corpus_inaugural %>% filter(Year >= 1900, Party == "Democratic") %>% summary(n = 10) ## Corpus consisting of 8 documents, showing 8 documents: ## ## Text Types Tokens Sentences Year President FirstName Party ## 1913-Wilson ... ## ... # Filter using a computed condition data_corpus_inaugural %>% filter(nchar(President) > 8) |> # long last names pull(President) ## [1] "Jefferson" "Jefferson" "Jefferson" "Madison" ... ``` -------------------------------- ### Extract a single document variable with pull() Source: https://context7.com/quanteda/quanteda.tidy/llms.txt Extracts one docvar from a corpus, tokens, or dfm object as a plain R vector. Accepts variable name, positive index, or negative index (default -1 = last column). ```r # Extract presidents of post-2000 speeches data_corpus_inaugural | filter(Year >= 2000) | pull(President) ``` ```r # Extract last docvar by default (-1) data_corpus_inaugural | slice_tail(n = 3) | pull() # last docvar (Party) ``` ```r # Works on tokens and dfm objects too toks <- data_corpus_inaugural |> tail() |> tokens() pull(toks, President) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.