0

I am using rtweet package in R to extract tweets of particular hashtag which basically needs appname,api_key,api_secret,access_token,access_token_secret. So i have created an app in Twitter to get all the above details. And then I pass the above as follows

 twitter_token <- create_token(
        app = appname,
        consumer_key = api_key,
        consumer_secret = api_secret)
tweets <-search_tweets("#iPhone8", n=1000, include_rts = FALSE, type = "recent",lang = "en")

So this will basically extract tweets with 88 variables. Now I want to write these tweets to an excel file for further analysis.

And for that I am using the below code.

tweets.df <-twListToDF(tweets)

When I run this I am getting the following error.

Error in twListToDF(tweets) : 
  Not all elements of twList are of the same class

Just want to understand what could be the potential issue here.

Problem is I cannot put the exact code here as that needs my api key and all.

Any help would be really appreciated.

Regards, Akash

Akash
  • 359
  • 1
  • 7
  • 27
  • `tweets` is already a data frame right? So why not do `write.csv(tweets, 'file/path/to/write)` ? Moreover, I do not see `twListToDF` function in `rtweet` package. As far as I can recall it was there in old `twitteR` package. – Ronak Shah Aug 13 '18 at 06:06
  • Thanks Ronak for quick reply. I am using twitteR for this function. When I try to write directly to csv, I get this error. Error in write.csv(tweets, file ='c:/Akash/Raw.csv', row.names = F ) unimplemented type 'list' in 'EncodeElement' – Akash Aug 13 '18 at 06:14
  • Can you check if any of the column is a list in `tweets` ? You need to `unlist` it first before writing it to csv I suppose. – Ronak Shah Aug 13 '18 at 06:20
  • and how do I check that ? There are 88 columns. So is there any way I can check it using commands ? – Akash Aug 13 '18 at 06:34
  • `sapply(tweets, class)` will give you all classes of all the columns. – Ronak Shah Aug 13 '18 at 06:44
  • Oh there are many list here.. e.g. Hastags column which will have data something like c("iphone8", "iphone", "apple") , so does that mean I will have to unlist all list columns and then export ? Also when I unlist, ideally it will create 3 records for each hashtag value ? I think there should be some other way to extract.. I am using this link to replicate. https://opensource.com/article/17/6/collecting-and-mapping-twitter-data-using-r – Akash Aug 13 '18 at 06:54
  • Are you sure you are using `rtweet` package? In that link they are using `twitteR` package. – Ronak Shah Aug 13 '18 at 06:58
  • I am using both, rtweet to extract tweets and twitteR to convert this into dataframe. Otherwise it will throw error that this function doesn't exists. – Akash Aug 13 '18 at 07:00
  • you don't need `twListToDF` function. Open a fresh new session and load only `rtweet` package and use `search_tweets` function from that package. The output of that should be data frame. – Ronak Shah Aug 13 '18 at 07:12
  • no luck.. I tried again.. – Akash Aug 13 '18 at 07:24
  • I think if we try to get the tweets of a particular user, then it will only have 16 variables and those does not include list and hence you are able to write it to csv. However here I am extracting tweets with a keyword which gives 88 variables including lists. But now when I tried to get tweets of a particular user also e.g. tweets <- userTimeline("realDonaldTrump", n=200) .. this gives another error Error in twInterfaceObj$doAPICall(cmd, params, method, ...) : OAuth authentication error: This most likely means that you have incorrectly called setup_twitter_oauth()' – Akash Aug 13 '18 at 08:49

1 Answers1

0

You can as well use this

tweets.df <-as.data.frame(tweets)
Yaseen Ahmad
  • 1,807
  • 5
  • 25
  • 43
J.Mursi
  • 1
  • 1