JDots JDots - 3 years ago 169
R Question

Unlist a data.frame column

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.

Answer Source

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

unnest(dataframe, nameofcolumns)



Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download