I have a data frame named "SpatialKey" with three columns. First column contains 5 categories representing population quintile. The second column has 4 kind of data: 0, 400, 800 and 1200. The third column represents population.
For example
| quintile | isocrona | total |
|---|---|---|
| 4 | 1200 | 1674 |
| 1 | 400 | 1676 |
| 4 | 400 | 1723 |
| 5 | 800 | 1567 |
| 3 | 0 | 1531 |
| 3 | 1200 | 1370 |
| 2 | 1200 | 1925 |
| 1 | 400 | 1916 |
| 5 | 0 | 1776 |
| 2 | 800 | 1896 |
| 3 | 800 | 2143 |
| 5 | 400 | 2098 |
| 4 | 400 | 1496 |
| 1 | 0 | 961 |
| 4 | 800 | 1684 |
I want to clasify the data by quintile and sum the population by the 4 kind of data I have in the second column. For example:
| 0 | 400 | 800 | 1200 | |
|---|---|---|---|---|
| 1 | 961 | 3592 | 0 | 0 |
| 2 | 0 | 0 | 1896 | 1925 |
| 3 | 1531 | 0 | 2143 | 1370 |
| 4 | 0 | 3219 | 1684 | 1674 |
| 5 | 1776 | 2098 | 1567 | 0 |
And here is my code.
po <- SpatialKey %>%
group_by(quintile, isocrona) %>%
summarise_at(vars(contains("total")), sum)
final_df <- as.data.frame(t(po))
But R give me the following table:
| V1 | V2 | V3 | V4 | V5 | V6 | V7 | V8 | V9 | V10 | V11 | V12 | V13 | V14 | V15 | V16 | V17 | V18 | V19 | V20 | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| quintile | 1 | 1 | 1 | 1 | 2 | 2 | 2 | 2 | 3 | 3 | 3 | 3 | 4 | 4 | 4 | 4 | 5 | 5 | 5 | 5 |
| isocrona | 0 | 400 | 800 | 1200 | 0 | 400 | 800 | 1200 | 0 | 400 | 800 | 1200 | 0 | 400 | 800 | 1200 | 0 | 400 | 800 | 1200 |
| total | 961 | 3592 | 0 | 0 | 0 | 0 | 1896 | 1925 | 1531 | 0 | 2143 | 1370 | 0 | 3219 | 1684 | 1674 | 1776 | 2098 | 1567 | 0 |
How would I do the second table in R?