I want to collapse some specific rows of a data.frame (preferably using dplyr in ). Collapsing should aggregate some columns by the functions sum(), others by mean().
As an example, let's add a unique character-based ID to the iris dataset.
iris_df <- iris[1:5,]
iris_df$ID <- paste("ID_",1:nrow(iris_df),sep="")
That's from where we start:
structure(list(Sepal.Length = c(5.1, 4.9, 4.7, 4.6, 5),
Sepal.Width = c(3.5, 3, 3.2, 3.1, 3.6),
Petal.Length = c(1.4, 1.4, 1.3, 1.5, 1.4),
Petal.Width = c(0.2, 0.2, 0.2, 0.2, 0.2),
Species = structure(c(1L, 1L, 1L, 1L, 1L),
.Label = c("setosa", "versicolor", "virginica"), class = "factor"),
ID = c("ID_1", "ID_2", "ID_3", "ID_4","ID_5")),
row.names = c(NA, 5L), class = "data.frame")
Now, I'd like to collapse the cases where ID==ID_1 + ID==ID_2. For that purpose, the Sepal values should be aggregated as means and the Petal values as sums. The ID should become "ID_1+ID_2" (so aggregation by paste()?)
This is how the final result should look like:
structure(list(Sepal.Length = c(5.0, 4.7, 4.6, 5),
Sepal.Width = c(3.25, 3.2, 3.1, 3.6),
Petal.Length = c(2.8, 1.3, 1.5, 1.4),
Petal.Width = c(0.4, 0.2, 0.2, 0.2),
Species = structure(c(1L, 1L, 1L, 1L),
.Label = c("setosa", "versicolor", "virginica"), class = "factor"),
ID = c("ID_1+ID_2", "ID_3", "ID_4","ID_5")),
row.names = c(NA, 4L), class = "data.frame")
Can this be done using dplyr (using group_by() and summarize()) package?
Update: As some additional note, the desired procedure should acknowledge that the row index are not known apriori, e.g. just that ID_x and ID_y need to be collapsed (and ID_x might be row i and ID_y at row j).