HomeTeam AwayTeam FTHG FTAG FTR HST AST
1 Aston Villa Wigan 0 2 A 5 7
26 Liverpool Aston Villa 1 3 A 12 4
34 Aston Villa Fulham 2 0 H 4 3
45 Birmingham Aston Villa 0 1 A 7 5
48 Aston Villa Portsmouth 2 0 H 5 12
58 Blackburn Aston Villa 2 1 H 7 5
76 Aston Villa Man City 1 1 D 6 7
I have a dataframe 'mydata' part of which is shown above. It shows the results of soccer games in England.
I want to sum the goals scored by a team such as Aston Villa, possibly by looping through the dataframe. This would require counting Villa's FTHG when they are the HomeTeam, and Villa's FTAG when they are the away team.
I tried
with(mydata, sum(FTHG[HomeTeam == "Aston Villa"] | FTAG[AwayTeam == "Aston Villa"]))
and
sum(subset(mydata,(HomeTeam == "Aston Villa")$FTHG|(AwayTeam == "Aston Villa")$FTAG))
and
sum(subset(mydata,(HomeTeam == "Aston Villa")$FTHG|subset(mydata,(AwayTeam == "Aston Villa")$FTAG)))
none of which worked.
What I want is the total to be shown. Can this be done without summing the home and away data separately then adding?