2015-11-29 66 views
0
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文章閱讀並想通了,將兩個SUMIF合併爲一條R語句

library(dplyr) 
Total <- fd%>% group_by(AwayTeam) %>% summarise(Teamaway = sum(awaywins)) 

會給我在客場比賽獲得的總分和

Total1 <- fd%>% group_by(HomeTeam) %>% summarise(HomeWins = sum(homewins)) 

將在主場比賽總給人點。但是,如何將這兩個語句合併爲一個並將其分配給一個變量。

任何人都可以請相同的幫助。

謝謝

+0

請使用dput顯示數據而不是圖像。 – akrun

+0

請發佈'data.frame'的'dput'以創建一個可重複的示例。 – tchakravarty

+0

你需要用'dput'的輸出來更新,而不是輸入'dput' – akrun

回答

0

我們可以使用data.table做到這一點。在轉換爲'data.table'(setDT(fd))之後,我們用melt將'wide'轉換爲'long'格式。 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')