Here's the data frame I'm trying to pivot, or rather, reshape:
Value Word list
1 c("cat", "dog")
1 c("apple", "banana")
2 c("cat", "dog")
2 c("peach", "orange")
3 c("cat", "dog")
3 c("berries", "coconut")
Here's the desired outcome (basically just combining elements with the same Value
to get one big list
by Value
):
Value Word list
1 c("cat", "dog", "apple", "banana")
2 c("cat", "dog", "peach", "orange")
3 c("cat", "dog", "berries", "coconut")
Thanks in advance to anyone who can offer help (and thank you everyone who have already commented/edited my poor post for me).
To give you an idea why I'm getting a list in my data frame, I'm actually doing a part of speech tagging. After breaking down the comment column with str_split, I got a list in my data frame, because the length of each comment varies. Each comment comes with a score, I need to create a bag of words data frame by the score.
Per your request, > str(df1):
'data.frame': 6 obs. of 2 variables:
$ Value : num 1 1 2 2 3 3
$ Wordlist:List of 6
..$ : chr "cat" "dog"
..$ : chr "apple" "banana"
..$ : chr "cat" "dog"
..$ : chr "peach" "orange"
..$ : chr "cat" "dog"
..$ : chr "berries" "coconut"
..- attr(*, "class")= chr "AsIs"
And > dput(df1):
structure(list(Value = c(1, 1, 2, 2, 3, 3), Wordlist = structure(list(
c("cat", "dog"), c("apple", "banana"), c("cat", "dog"), c("peach",
"orange"), c("cat", "dog"), c("berries", "coconut")), class = "AsIs")), .Names = c("Value", "Wordlist"), row.names = c(NA, -6L), class = "data.frame")