Re: [R] Trouble with Functions
Haha no, TextWrangler. And that was definitely it...I think what was happening is that when I opened the text version of the book it opened in Notepad, which was probably opened the txt file in RTF. Then I copied and pasted the function code into TextWrangler and didn't even think about Smart Quotes. So I used the Straighten Quote feature. It got through all the way to the last line where I got an unexpected string error: Error in source(/Documents/score.txt) : /Documents/score.txt:13:25: unexpected INCOMPLETE_STRING 32: return(scores.df) 33: } Is there a debug version I could be running or something that lists more descriptive error explanations? That way I don't have to bother you guys and embarrass myself so. :-) Thank you for your help! I appreciate it. On Wed, Jun 6, 2012 at 2:29 PM, Peter Ehlers [via R] ml-node+s789695n4632578...@n4.nabble.com wrote: On 2012-06-05 17:00, dougmcintosh wrote: FYI - here are the errors (I can get rid of the first one by removing the progress parameter - which I understand controls whether a progress bar is displayed). The second one stops at the gsub call. Maybe if someone could give me a properly formatted example I could work from instead? Thanks! (the caret is underneath the ? after progress in the first case, and under the parenthesis in the second) source('/Documents/score.txt') Error in source(Documents/score.txt) : /Users/dougmcintosh/Documents/score.txt:1:73: unexpected input 1: score.sentiment = function(sentences, pos.words, neg.words, .progress = ? source('/Documents/score.txt') Error in source(Documents/score.txt) : /Users/dougmcintosh/Documents/score.txt:15:17: unexpected input 14: 15: sentence = gsub(? I think that your problems may lie with an inappropriate editor that's using 'smart' quotes. Please say you're not using Word. Peter Ehlers __ [hidden email] http://user/SendEmail.jtp?type=nodenode=4632578i=0mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- If you reply to this email, your message will be added to the discussion below: http://r.789695.n4.nabble.com/Trouble-with-Functions-tp4632456p4632578.html To unsubscribe from Trouble with Functions, click herehttp://r.789695.n4.nabble.com/template/NamlServlet.jtp?macro=unsubscribe_by_codenode=4632456code=ZG91Zy5tY2ludG9zaEBnbWFpbC5jb218NDYzMjQ1NnwxMjA3ODQzNjMx . NAMLhttp://r.789695.n4.nabble.com/template/NamlServlet.jtp?macro=macro_viewerid=instant_html%21nabble%3Aemail.namlbase=nabble.naml.namespaces.BasicNamespace-nabble.view.web.template.NabbleNamespace-nabble.view.web.template.NodeNamespacebreadcrumbs=notify_subscribers%21nabble%3Aemail.naml-instant_emails%21nabble%3Aemail.naml-send_instant_email%21nabble%3Aemail.naml -- View this message in context: http://r.789695.n4.nabble.com/Trouble-with-Functions-tp4632456p4632579.html Sent from the R help mailing list archive at Nabble.com. [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] Trouble with Functions
Hi guys, I'm a new to R and following along with Tutorials using this book: http://www.amazon.com/Practical-Statistical-Analysis-Non-structured-Applications/dp/012386979X In one of them, they use the twitteR package and describe the following function (see below). From what I can tell from the documentation (R), there's a method to call it directly in an interactive session. The way it's presented in the book, however, it appears it's formatted for a text file that (I'm assuming) you call using source(). At any rate, when I read it into the session using source I get errors and I can't find documentation on what's wrong (or tell from the feedback). Hoping you can help! /Implementing Our Sentiment Scoring Algorithm To score each tweet, our score.sentiment() function uses laply() to iterate through the input text. It strips punctuation and control characters from each line using R’s regular expression-powered substitution function, gsub() and uses match() against each word list to find matches:/ score.sentiment = function(sentences, pos.words, neg.words, .progress=’none’) { require(plyr) require(stringr) # we got a vector of sentences. plyr will handle a list # or a vector as an “l” for us # we want a simple array of scores back, so we use # “l” + “a” + “ply” = “laply”: scores = laply(sentences, function(sentence, pos.words, neg.words) { # clean up sentences with R’s regex-driven global substitute, gsub(): sentence = gsub(‘[[:punct:]]’, ”, sentence) sentence = gsub(‘[[:cntrl:]]’, ”, sentence) sentence = gsub(‘\\d+’, ”, sentence) # and convert to lower case: sentence = tolower(sentence) # split into words. str_split is in the stringr package word.list = str_split(sentence, ‘\\s+’) # sometimes a list() is one level of hierarchy too much words = unlist(word.list) # compare our words to the dictionaries of positive negative terms pos.matches = match(words, pos.words) neg.matches = match(words, neg.words) # match() returns the position of the matched term or NA # we just want a TRUE/FALSE: pos.matches = !is.na(pos.matches) neg.matches = !is.na(neg.matches) # and conveniently enough, TRUE/FALSE will be treated as 1/0 by sum(): score = sum(pos.matches) - sum(neg.matches) return(score) }, pos.words, neg.words, .progress=.progress) scores.df = data.frame(score=scores, text=sentences) return(scores.df) } -- View this message in context: http://r.789695.n4.nabble.com/Trouble-with-Functions-tp4632456.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Trouble with Functions
FYI - here are the errors (I can get rid of the first one by removing the progress parameter - which I understand controls whether a progress bar is displayed). The second one stops at the gsub call. Maybe if someone could give me a properly formatted example I could work from instead? Thanks! (the caret is underneath the ? after progress in the first case, and under the parenthesis in the second) source('/Documents/score.txt') Error in source(Documents/score.txt) : /Users/dougmcintosh/Documents/score.txt:1:73: unexpected input 1: score.sentiment = function(sentences, pos.words, neg.words, .progress = ? source('/Documents/score.txt') Error in source(Documents/score.txt) : /Users/dougmcintosh/Documents/score.txt:15:17: unexpected input 14: 15: sentence = gsub(? -- View this message in context: http://r.789695.n4.nabble.com/Trouble-with-Functions-tp4632456p4632460.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.