Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

R function as() lapply() rbind() and do.call() for use in TweetFrame() #60

Open
jmaccherone opened this issue May 22, 2013 · 1 comment

Comments

@jmaccherone
Copy link
Collaborator

as() performs a type coercion: in other words it changes one type to another type.

lapply() applies a function onto all of the elements of a list.

In the command below, lapply(tweetList, as.data.frame), applies the as.data.frame() coercion to each element in tweetList.

the rbind() function “binds” together the elements that are supplied to it into a row-by-row structure.

the do.call() function executes a function call, but unlike just running the function from the console, allows for a variable number of arguments to be supplied to the function.

The whole command we will use looks like this:
tweetDF <- do.call("rbind", lapply(tweetList, +
 as.data.frame))

as.data.frame() coerces each list element into a row

lapply() applies this to all of the elements in tweetList

rbind() takes all of the rows and puts them together

do.call() gives rbind() all the rows as individual elements

This takes a list of 500 tweet items (tweetList) and makes it a rectangular data frame. Each of the 500 tweet items had 10 fields, so now we can treat it as 500 observations of 10 variables.

Calling rbind this way sends the 500 results of lapply() to rbind to make one data frame instead of making each tweet into a list.

  • TweetFrame() - Return a dataframe based on a
 # search of Twitter

TweetFrame<-function(searchTerm, maxTweets)
{
twtList<-searchTwitter(searchTerm,n=maxTweets)
return(do.call("rbind",+
 lapply(twtList,as.data.frame)))
}

@nwwangnan
Copy link

Nice explanations!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants