Skip to content

Instantly share code, notes, and snippets.

@Robinlovelace
Created October 27, 2014 15:26
Show Gist options
  • Save Robinlovelace/ab68081c1869d008a920 to your computer and use it in GitHub Desktop.
Save Robinlovelace/ab68081c1869d008a920 to your computer and use it in GitHub Desktop.
Reduce max. number of words in a text string, keeping only first
# Function to reduce the max. number of words in a string
maxwords <- function(x, max = 10){
lwords <- length(x)
if(lwords > max) lwords <- max
paste0(x[1:lwords], collapse = " ")
}
# Apply maxwords to the data
tdft$text <- sapply(words, maxwords)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment