For a sample dataframe:
df <- structure(list(output.code = structure(1:15, .Label = c("E00000001",
"E00000003", "E00000005", "E00000007", "E00000010", "E00000012",
"E00000013", "E00000014", "E00000016", "E00000017", "E00000018",
"E00000019", "E00000020", "E00000021", "E00000022"), class = "factor"),
all.usual = c(194L, 250L, 367L, 123L, 102L, 213L, 216L, 154L,
281L, 290L, 218L, 139L, 226L, 282L, 223L), same.address = c(176L,
218L, 288L, 83L, 80L, 196L, 134L, 125L, 228L, 218L, 189L,
112L, 185L, 235L, 192L), lsoa.code = structure(c(1L, 1L,
1L, 1L, 3L, 3L, 3L, 3L, 2L, 2L, 2L, 2L, 2L, 2L, 3L), .Label = c("E01000001",
"E01000002", "E01000003"), class = "factor")), .Names = c("output.code",
"all.usual", "same.address", "lsoa.code"), class = "data.frame", row.names = c(NA,
-15L))
I am trying to aggregate two columns into a new dataframe using dplyr.
For one column, I found this to work:
library(dplyr)
df %>%
group_by(lsoa.code) %>%
summarise(all.usual = sum(all.usual))
But I wish to sum the values of all the 'lsoa.codes' for the two variables (same.address and all.usual).
This doesn't work:
df %>%
group_by(lsoa.code) %>%
summarise(all.usual = sum(all.usual)
summarise(same.address = sum(same.address))
Can someone please advise how this code may be adapted to aggregate the two columns?
Also, I wish to create a dataframe from the results.
Many thanks in advance.