Skip to content

Instantly share code, notes, and snippets.

@Selbosh
Last active March 15, 2018 11:57
Show Gist options
  • Save Selbosh/9324a5b5d9a98f2d0af922c40b45f548 to your computer and use it in GitHub Desktop.
Save Selbosh/9324a5b5d9a98f2d0af922c40b45f548 to your computer and use it in GitHub Desktop.
Minimal working example of un-nesting a data frame
# Create data frame with a list column
users <- data.frame(
id = 1:3,
username = c('Tom', 'Dick', 'Harry'),
following = I(list(
c('TeaStats', 'RobertJBlincoe'),
c('JeremyCorbyn', 'realDonaldTrump'),
c('Scottish_Tweets')
))
)
# Show the nested data
users
# Unnest any/all list columns
tidyr::unnest(users)
@Selbosh
Copy link
Author

Selbosh commented Mar 15, 2018

Nested output (users)

  id username                     following
1  1      Tom      TeaStats, RobertJBlincoe
2  2     Dick JeremyCorbyn, realDonaldTrump
3  3    Harry               Scottish_Tweets

Unnested output (unnest(users))

  id username       following
1  1      Tom        TeaStats
2  1      Tom  RobertJBlincoe
3  2     Dick    JeremyCorbyn
4  2     Dick realDonaldTrump
5  3    Harry Scottish_Tweets

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment