lmaccherone / Lumenize

Illuminating the forest AND the trees in your data
https://cdn.rawgit.com/lmaccherone/Lumenize/v1.0.3/docs/lumenize-docs/index.html
38 stars 11 forks source link

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

Open jmaccherone opened 11 years ago

jmaccherone commented 11 years ago

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<-function(searchTerm, maxTweets) { twtList<-searchTwitter(searchTerm,n=maxTweets) return(do.call("rbind",+
 lapply(twtList,as.data.frame))) }

nwwangnan commented 6 years ago

Nice explanations!