Lumenize icon indicating copy to clipboard operation
Lumenize copied to clipboard

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

Open jmaccherone opened this issue 12 years ago • 1 comments

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))) }

jmaccherone avatar May 22 '13 15:05 jmaccherone

Nice explanations!

nwwangnan avatar Jan 27 '18 02:01 nwwangnan