Gabriele B
Gabriele B

Reputation: 2685

Unlist (flatten lists) row by row in a data frame using R

I have a list of tweets in a data.frame and I can extract lists of hashtags from them using

> rpg.twitter.df$hashtags <-
  regmatches(rpg.twitter.df$text,gregexpr("#(\\d|\\w)+",rpg.twitter.df$text))

It ends up with one list for each row. Now, I want to flatten each list in comma-separated strings (one for each row)

I tried this:

do.call("paste", c(rpg.twitter.df$hashtags, sep=", "))

but it doesn't work as it ends up with one huge vector. Same if i enclose regmatches with unlist(..., recursive=FALSE)

any idea on how to solve it?

Some data for a reproducible example:

rpg.twitter.df <- data.frame(text=rbind("World of Warcrack: http://t.co/3MNRpArnGw #wow #WorldOfWarcraft #warcraft #mmorpg #rpg #RPGChat #gaming #pcgaming #online @WoW_en @NewsWoW", "@ashleythedragon join my journey in Tweeria http://t.co/CFKDLA3ASE #rpg", "How to use of #RPG  for motivation  #timeboxing  http://t.co/mwwN5xErHx"))

Upvotes: 2

Views: 2824

Answers (2)

agstudy
agstudy

Reputation: 121618

You can also use this :

 toString(rpg.twitter.df$hashtags)

Upvotes: 2

flodel
flodel

Reputation: 89097

You can do:

sapply(rpg.twitter.df$hashtags, paste, collapse = ",")

Upvotes: 3

Related Questions