I have a long dataset with this structure:
df <- data.frame(
stringsAsFactors = FALSE,
Code = c("05-001-01-001-U",
"05-001-01-001-U","05-001-01-001-U","05-002-01-001-A",
"05-002-01-001-A","05-002-01-001-A","05-002-01-001-B",
"05-002-01-001-B","05-002-01-001-B","05-002-01-002-A",
"05-002-01-002-A","05-002-01-002-A","05-002-01-002-B",
"05-002-01-002-B","05-002-01-002-B"),
Party = c("Party1","Party2","Party3",
"Party1","Party2","Party3","Party1","Party2","Party3",
"Party1","Party2","Party3","Party1","Party2",
"Party3"),
Value = c(1L,41L,2L,7L,99L,7L,3L,
82L,4L,1L,76L,1L,2L,76L,2L)
I want to achieve two things:
In the column code, join all the rows with the same data: if the code is 05-001-01-001-U and there is two rows, obtain a single row with this Code and without the 'U' and without the '-' symbol.
In the rows like "05-002-01-001-A" with A or B also merge all data (six rows in this case) in one single row: 0500201001.
Then, with the party and value colum, sum each party value for his unique code. Something like this:
data.frame(
stringsAsFactors = FALSE,
Code = c("0500201001"),
Party1 = c(10L),
Party2 = c(181L),
Party3 = c(11L)
)
I don't know if I have explained well what I want to achieve. Don't see a solution, I am grateful for any hints on how to do it.