Home > Software engineering >  How do I create separate dataframes of tweets using identifiers from another dataframe?
How do I create separate dataframes of tweets using identifiers from another dataframe?

Time:07-05

I am still learning R and am trying to create a database of twitters of political representatives using rtweet. I have a dataframe with the Twitter handles of hundreds of such representatives, the region they represent and their political affiliation.

tweets <- data.frame(Person = c("A", "B", "C"), 
                     Handle = c("@RepA", "@RepB", "@RepC"),
                      Party = c("AA", "CO", "BJ"), 
                     Region = c("P", "D", "R"))

I want to create a separate dataframe of each person's twitter account which includes their respective parties and regions. So far, I have done it manually using the following code:

repA        <- get_timelines('RepA', n=3200, lang="en")
repA$party  <- "AA"
repA$region <- "P"
repB        <- get_timelines('RepB', n=3200, lang="en")
repB$party  <- "CO"
repB$region <- "D"
repC        <- get_timelines('RepC', n=3200, lang="en")
repC$party  <- "BJ"
repC$region <- "R"

But I have at least 500 entries in the original dataframe and would like to automate this process. There must be a cleaner way to do this?

CodePudding user response:

users<-substring(tweets$Handle, 2 , nchar(tweets$Handle))

timelines<-lapply(users, function(u) {
tl<-get_timelines(u, n=3200, lang="en")
tl$user<-u
tl$party<-tweets[tweets$Handle==paste0("@",u),"Party"]
tl$region<-tweets[tweets$Handle==paste0("@",u),"Region"]
})

data.table::rbindlist(timelines)
  • Related