How to merge multiple rows against a unique id into one single row in R?

Viewed 25

I have a dataframe with two columns (username and tweet). username has multiple repeating values and tweet contains different tweets that the user have posted. For example:

username   tweet
user_a     This is first tweet.
user_a     This is second tweet.
user_b     It is a good weather today.
user_b     It will rain.

For all the tweets that each user have posted, I want them to be in a single row. For example, the above dataframe should look something like this:

username   tweet
user_a     This is first tweet. This is second tweet.
user_b     It is a good weather today. It will rain.

Any guidance on how to do that please?

1 Answers

You can use aggregate like this:

aggregate(tweetsData, tweet ~ username , paste, collapse=" ")

  username                                      tweet
1   user_a This is first tweet. This is second tweet.
2   user_b  It is a good weather today. It will rain.
Related