I have a dataframe in R that in which I want to merge certain rows and combine the values of certain cells in these rows. Imagine the following data frame:
Col.1<-c("a","b","b","a","c","c","c","d")
Col.2<-c("mouse", "cat", "dog", "bird", "giraffe", "elephant", "zebra", "worm")
df<-data.frame(Col.1, Col.2)
df
Col.1 Col.2
a mouse
b cat
b dog
a bird
c giraffe
c elephant
c zebra
d worm
I would like to merge all adjacent rows in which the values in Col.1 are the same and combine the values in Col.2 accordingly.
The final result should look like this:
Col.1 Col.2
a mouse
b cat dog
a bird
c giraffe elephant zebra
d worm
I have tried to use a dplyr-solution (like:ddply(df, .(Col.1), summarize, Col.2 = sum(Col.2))
), but the sum-command doesn't work for factor-values.