One way is to use the aggregate()
function. From ?aggregate
:
Splits the data into subsets, computes summary statistics for each,
and returns the result in a convenient form
First, read in your data (you should do this in your question in the future to provide a reproducible example, see: How to make a great R reproducible example?):
txt <- "user1, hashtag1, hashtag2
user1, hashtag3, hashtag4
user2, hashtag5, hashtag6
user2, hashtag7, hashtag8"
x <- read.delim(file = textConnection(txt), header = F, sep = ",",
strip.white = T, stringsAsFactors = F)
Then, use aggregate()
to split the data into subsets and convert each subset to a 1-dimensional array:
aggregate(x[-1], by = x[1], function(z)
{
dim(z) <- c(length(z)) # Change dimensions of z to 1-dimensional array
z
})
# V1 V2.1 V2.2 V3.1 V3.2
# 1 user1 hashtag1 hashtag3 hashtag2 hashtag4
# 2 user2 hashtag5 hashtag7 hashtag6 hashtag8
Edit
This approach only works if all users have the same number of hashtags, which seems unlikely. @Josh O'Brien's answer is the better approach.