Q:

Creating dummy variable if the key word in the list

I faced the following problem:
i am trying to create a dummy variable that will print 1 in case if team is top and 0 otherwise. The list of top teams is : (Arsenal, Chelsea, ManCity, ManUni, Liverpool)
Original data season_2015:
                    team       opponent goals home 
HomeTeam         Arsenal Crystal Palace     2    1   
AwayTeam  Crystal Palace        Arsenal     1    0   
HomeTeam1      Leicester        Everton     2    1   
AwayTeam1        Everton      Leicester     2    0   
HomeTeam2     Man United        Swansea     1    1   
AwayTeam2        Swansea     Man United     2    0   

My aim is to create variable "top" that will put 1 if team is in the wordlist.
                    team       opponent goals home top
HomeTeam         Arsenal Crystal Palace     2    1   1
AwayTeam  Crystal Palace        Arsenal     1    0   0
HomeTeam1      Leicester        Everton     2    1   0
AwayTeam1        Everton      Leicester     2    0   0
HomeTeam2     Man United        Swansea     1    1   1
AwayTeam2        Swansea     Man United     2    0   0

The code i have tried is (i know the code is wrong):
wordlist <- c("Arsenal", "Man City", "Man United", "Liverpool", "Chelsea")
for(i in wordlist){season_2015[paste("top")] <- ifelse(top == wordlist,1,0)}

I am new to R, any help is appreciated. Thanks in advance.

A:

If your data looks something like this
>season_2015 <- read.csv("season_2015.csv")
>season_2015

  home_away           team       opponent goals home
1  HomeTeam        Arsenal Crystal Palace     2    1
2  AwayTeam Crystal Palace        Arsenal     1    0
3 HomeTeam1      Leicester        Everton     2    1
4 AwayTeam1        Everton      Leicester     2    0
5 HomeTeam2     Man United        Swansea     1    1
6 AwayTeam2        Swansea     Man United     2    0

And your top teams data looks like this:
>topteams <- c("Arsenal", "Man City", "Man United", "Liverpool", "Chelsea")

Then you can do this:
>season_2015$top <- ifelse(season_2015$team %in% topteams, 1, 0)
>season_2015

  home_away           team       opponent goals home top
1  HomeTeam        Arsenal Crystal Palace     2    1   1
2  AwayTeam Crystal Palace        Arsenal     1    0   0
3 HomeTeam1      Leicester        Everton     2    1   0
4 AwayTeam1        Everton      Leicester     2    0   0
5 HomeTeam2     Man United        Swansea     1    1   1
6 AwayTeam2        Swansea     Man United     2    0   0

