Created
June 10, 2014 18:58
-
-
Save stephaniehicks/5e43ba6448ed28a8dce5 to your computer and use it in GitHub Desktop.
Clean the tweets from searchTwitter()
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
clean.tweets <- function(some_txt) | |
{ | |
some_txt = gsub("&", "", some_txt) | |
some_txt = gsub("(RT|via)((?:\\b\\W*@\\w+)+)", "", some_txt) | |
some_txt = gsub("@\\w+", "", some_txt) | |
some_txt = gsub("[[:punct:]]", "", some_txt) | |
some_txt = gsub("[[:digit:]]", "", some_txt) | |
some_txt = gsub("http\\w+", "", some_txt) | |
some_txt = gsub("[ \t]{2,}", "", some_txt) | |
some_txt = gsub("^\\s+|\\s+$", "", some_txt) | |
# define "tolower error handling" function | |
try.tolower = function(x) | |
{ | |
y = NA | |
try_error = tryCatch(tolower(x), error=function(e) e) | |
if (!inherits(try_error, "error")) | |
y = tolower(x) | |
return(y) | |
} | |
some_txt = sapply(some_txt, try.tolower) | |
some_txt = some_txt[some_txt != ""] | |
names(some_txt) = NULL | |
return(some_txt) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment