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")
在上述数据中,我试图找出所有球队在主场和客场所获得的总积分。我阅读了有关SUMIF的文章并想出了这一点,
library(dplyr)
Total <- fd%>% group_by(AwayTeam) %>% summarise(Teamaway = sum(awaywins))
会给我在客场比赛中获得的总积分和
Total1 <- fd%>% group_by(HomeTeam) %>% summarise(HomeWins = sum(homewins))
会给出主场比赛的总得分。但是,如何将这两个语句合并为一个并将其分配给变量。
任何人都可以帮忙。
谢谢
答案 0 :(得分:0)
我们可以使用data.table
来执行此操作。转换为'data.table'(setDT(fd)
)后,我们将{wide'格式转换为'long'格式melt
。 data.table melt
还可以选择使用带有measure
参数的多个patterns
列。然后,我们使用dcast
将数据集转换回“广泛”格式,并将fun.aggregate
用作sum
。
library(data.table)#v1.9.6+
dcast(melt(setDT(fd), measure=patterns('Team', 'win'))[,
sum(value2), .(value1,variable)], value1~variable, value.var='V1')