structure(list(Date = structure(c(48L, 48L, 48L, 48L, 48L, 48L,
48L, 50L, 50L, 53L), .Label = c("01/01/14", "01/02/14", "01/03/14",
"01/09/13", "01/12/13", "02/02/14", "02/03/14", "02/11/13", "03/02/14",
"03/05/14", "03/11/13", "03/12/13", "04/05/14", "04/12/13", "05/04/14",
"05/05/14", "05/10/13", "06/04/14", "06/05/14", "06/10/13", "07/04/14",
"07/05/14", "07/12/13", "08/02/14", "08/03/14", "08/12/13", "09/02/14",
"09/11/13", "09/12/13", "10/11/13", "11/01/14", "11/02/14", "11/05/14",
"12/01/14", "12/02/14", "12/04/14", "13/01/14", "13/04/14", "14/09/13",
"14/12/13", "15/03/14", "15/04/14", "15/09/13", "15/12/13", "16/03/14",
"16/04/14", "16/09/13", "17/08/13", "18/01/14", "18/08/13", "19/01/14",
"19/04/14", "19/08/13", "19/10/13", "20/01/14", "20/04/14", "20/10/13",
"21/04/14", "21/08/13", "21/09/13", "21/10/13", "21/12/13", "22/02/14",
"22/03/14", "22/09/13", "22/12/13", "23/02/14", "23/03/14", "23/11/13",
"23/12/13", "24/08/13", "24/11/13", "25/03/14", "25/08/13", "25/11/13",
"26/03/14", "26/04/14", "26/08/13", "26/10/13", "26/12/13", "27/04/14",
"27/10/13", "28/01/14", "28/04/14", "28/09/13", "28/12/13", "29/01/14",
"29/03/14", "29/09/13", "29/12/13", "30/03/14", "30/09/13", "30/11/13",
"31/03/14", "31/08/13"), class = "factor"), HomeTeam = structure(c(1L,
9L, 13L, 16L, 17L, 19L, 20L, 4L, 5L, 10L), .Label = c("Arsenal",
"Aston Villa", "Cardiff", "Chelsea", "Crystal Palace", "Everton",
"Fulham", "Hull", "Liverpool", "Man City", "Man United", "Newcastle",
"Norwich", "Southampton", "Stoke", "Sunderland", "Swansea", "Tottenham",
"West Brom", "West Ham"), class = "factor"), AwayTeam = structure(c(2L,
15L, 6L, 7L, 11L, 14L, 3L, 8L, 18L, 12L), .Label = c("Arsenal",
"Aston Villa", "Cardiff", "Chelsea", "Crystal Palace", "Everton",
"Fulham", "Hull", "Liverpool", "Man City", "Man United", "Newcastle",
"Norwich", "Southampton", "Stoke", "Sunderland", "Swansea", "Tottenham",
"West Brom", "West Ham"), class = "factor"), FTR = structure(c(1L,
3L, 2L, 1L, 1L, 1L, 3L, 3L, 1L, 3L), .Label = c("A", "D", "H"
), class = "factor"), homewins = c(0, 3, 1, 0, 0, 0, 3, 3, 0,
3), awaywins = c(3, 0, 1, 3, 3, 3, 0, 0, 3, 0)), .Names = c("Date",
"HomeTeam", "AwayTeam", "FTR", "homewins", "awaywins"), row.names = c(NA,
10L), class = "data.frame")
In the above data, I am trying to find out the total points earned by all the teams at home and away. I read through the articles for SUMIF and figured out that,
library(dplyr)
Total <- fd%>% group_by(AwayTeam) %>% summarise(Teamaway = sum(awaywins))
would give me the total points earned in the away matches and
Total1 <- fd%>% group_by(HomeTeam) %>% summarise(HomeWins = sum(homewins))
would give the total points in the home matches. But how do I combine both of these statements into one and assign it to a variable.
Can anyone please help with the same.
Thank you
We could use data.table
to do this. After converting to 'data.table' (setDT(fd)
), we convert the 'wide' to 'long' format with melt
. The data.table melt
also have the option to use multiple measure
columns with the patterns
argument. Then, we convert the dataset back to 'wide' format with dcast
and use the fun.aggregate
as sum
.
library(data.table)#v1.9.6+
dcast(melt(setDT(fd), measure=patterns('Team', 'win'))[,
sum(value2), .(value1,variable)], value1~variable, value.var='V1')