2

I have a data.frame that contains the data of a facebook page with 167 objects

I then take the "message" column and separate the hashtags from it with stringr. This produces a list called "hashes" of 67.

msg <- page$message
hashes <- str_extract_all(msg,"#\\S+")

I then append hashes to an empty column I created, called hashtags.

page$hashtags <- NA
page$hashtags <- hashes

However the column "hashtags" is now a list. If I unlist(page$hashtags) everything turns into NA.

Conversely, if I unlist "hashes", I can't find a way to re-append this to the data.frame because they have different numbers of objects.

I feel like I'm missing something simple and need something from the apply family or dplyr?

Any help is greatly appreciated.

BENY
  • 317,841
  • 20
  • 164
  • 234
JDots
  • 45
  • 1
  • 7

1 Answers1

9

For unlisting list-columns, you need to call unnest from the tidyr package.

unnest(dataframe, nameofcolumns)

Best,

Colin

Colin FAY
  • 4,849
  • 1
  • 12
  • 29
  • Always glad to help! If it works, you can [accept the answer](https://stackoverflow.com/help/someone-answers).Thanks, Colin. – Colin FAY Jul 25 '17 at 17:25
  • Hey Colin, question for you. Using unnest returns a data.frame of 61 rows. How can I unnest and preserve the entire 167 rows of the original data.frame? – JDots Jul 25 '17 at 18:05
  • So concise and clean-- this is exactly what I was looking for! Thanks for sharing!!! – philiporlando Nov 23 '20 at 23:55