Getting Text From Tweets

2019-09-09 07:46发布

I am tring to read my tweets from a csv file (which I have downloaded previously), and I am having some problems:

sia.list <- searchTwitter('#singaporeair', n=10, since=NULL, until=NULL, cainfo="cacert.pem")  
sia.df = twListToDF(sia.list)  
write.csv(sia.df, file='C:/temp/siaTweets.csv', row.names=F)

I am trying to extract the text from the list and the problems is with the third line below:

sia.df <- read.csv(file=paste(path,"siaTweets.csv",sep="")) 
sia.list <- as.list(t(sia.df))
sia_txt = sapply(sia.list, function(x) x$getText())

console output:

   > sia.list <- as.list(t(sia.df))
   > sia_txt = sapply(sia.list, function(x) x$getText())
    Error in x$getText : $ operator is invalid for atomic vectors

2条回答
The star\"
2楼-- · 2019-09-09 08:28

If you want to read the text from a csv file , all you have to do is : sia_txt <- sia$text (text being the name of the column in which your text is stored.)

the x$getText you've used in saaply is a method available only to lists which come as result of searchTwitter(). Hence you cannot convert a DF back to List and use getText. For e.g. do:

xyz <- searchTwitter("#xyz", n = 100)

str(xyz)

You will see a list of methods applicable. something like:

$ :Reference class 'status' [package "twitteR"] with 17 fields ..$ text : chr "RT @BET: \"Who's left to love the black woman?\" ~ WATCH last night's #BeingMaryJane here:\nhttp://t.co/xiUho1FVQi http://t.co/"| __truncated__ ..$ favorited : logi FALSE ..$ favoriteCount: num 0 ..$ replyToSN : chr(0) ..$ created : POSIXct[1:1], format: "2015-03-11 13:28:01" ..$ truncated : logi FALSE ..$ replyToSID : chr(0) ..$ id : chr "575649378062434304" ..$ replyToUID : chr(0) ..$ statusSource : chr "<a href=\"http://twitter.com\" rel=\"nofollow\">Twitter Web Client</a>" ..$ screenName : chr "kookie_kay" ..$ retweetCount : num 20 ..$ isRetweet : logi TRUE

and 51 methods, of which 39 are possibly relevant: .. getCreated, getFavoriteCount, getFavorited, getId, getIsRetweet, getLatitude, getLongitude, getReplyToSID, .. getReplyToSN, getReplyToUID, getRetweetCount, getRetweeted, getRetweeters, getRetweets, getScreenName, .. getStatusSource, **getText**, getTruncated, getUrls, initialize

Notice the getText.

查看更多
劳资没心,怎么记你
3楼-- · 2019-09-09 08:32

x$getText() doesn't really make sense here. Because you are using sapply(), each element of sia.list is what gets passed to x, and these don't have sub-elements you can access using the $ operator.

What is getText()? It's not a function in base r or in the twitteR package. If it's a function from some other package, maybe getText(x) is what you want, but it's hard to say without knowing more about where it comes from.

查看更多
登录 后发表回答