Error HELP!! "no applicable method for 'select_' applied to an object of class "function" "

CODE BELOW

EPL_Standings <- function(date, season){

options(dplyr.summarise.inform = FALSE)

#Data Frames
url = paste("http://www.football-data.co.uk/mmz4281/",gsub("/","",substr(season, 3,7)),"/E0.csv",sep="")
EPL_data = read.csv(url)

#Select the columns needed for assignment
select(Date,HomeTeam,AwayTeam,FTHG,FTAG,FTR)%>%

#Use lubridate to change the date to read Month/Day/Year
mutate(Date = dmy(Date))%>%
filter(Date <= mdy(date))

#Make a column called 'Team'
Home <- EPL_data%>%
  select(Date,Team = HomeTeam,FTHG,FTAG,FTR)%>%
  mutate(HomeAway = 'Home')
Away <- EPL_data%>%
  select(Date,Team = AwayTeam,FTHG,FTAG,FTR)%>%
           mutate(HomeAway = 'Away')
#Join these sets
team_data <- bind_rows(Home, Away)%>%
  arrange(Team, desc(Date))%>%
#Check to see if the team won or lost
mutate(Home_Win = ifelse (HomeAway == 'Home' & FTHG > FTAG, 1, 0), 
       Away_Win = ifelse (HomeAway == 'Away' & FTAG > FTHG, 1, 0),
       Home_Tie = ifelse (HomeAway == 'Home' & FTHG == FTAG, 1, 0),
       Away_Tie = ifelse (HomeAway == 'Away' & FTAG == FTHG, 1, 0),
       Home_Loss = ifelse (HomeAway == 'Home' & FTHG < FTAG, 1, 0),
       Away_Loss = ifelse (HomeAway == 'Away' & FTAG < FTHG, 1, 0),
       #Goals Scored for Home and Away
       Goals_Scored = ifelse (HomeAway == 'Home', FTHG, FTAG),
       #Goals Allowed = Goals by the away team
       Goals_Allowed = ifelse (HomeAway == 'Home', FTAG, FTHG),
       #Returns 1 for win, 0 for loss
       Wins1 = ifelse (Home_Win == 1 | Away_Win == 1, 1, 0),
       #Returns 1 for loss, 0 for win
       Losses1 = ifelse (Home_Loss == 1 | Away_Loss == 1, 1, 0),
       #Returns 1 if tie (Draw), 0 if not
       Tie = ifelse (Home_Tie == 1 | Away_Tie == 1, 1, 0),)%>%
  #Grouping by team
  group_by(Team)%>%
  
#Using variables created above to calculate the following:
          #Wins is the sum of Wins1 created above
summarize(Wins = sum(Wins1),
          #Ttl Losses is the sum of Losses1 created above
          Losses = sum(Losses1),
          #Ttl Draws is the sum of Tie(s) created above
          Draws = sum(Tie),
          #Ttl Wins by the Home team created above
          HomeWins = sum(Home_Win),
          #Ttl Losses by the Home Team created above
          HomeLosses = sum(Home_Loss),
          #Ttl Draws for the Home Team by using Home_Tie created above
          HomeDraws = sum(Home_Tie),
          #Ttl wins by the Away team from Away_Win created above
          AwayWins = sum(Away_Win),
          #Ttl Losses by the Away team from the Away_Loss created above
          AwayLosses = sum(Away_Loss),
          #Ttl Draws by the Away team from Away_tie created above
          AwayDraws = sum(Away_Tie),
          #Ttl Goals Scored for the season using Goals_Scored created above 
          GS = sum(Goals_Scored),
          #Ttl Goals Allowed (scored by the other team by using Goals_Allowed created above)
          GA = sum(Goals_Allowed),
          #Calculated Points by multiplying the wins by 3, and then adding the total ties (each worth 1 point)
          Points = (3 * sum(Wins1)) + sum(Tie),
          #Getting the matches played by each team
          MatchesPlayed = n(),
          #The following helps us get the team's record over the last 10 games
          Last10Wins = sum(Wins1 [1:10]),
          Last10Losses = sum(Losses1 [1:10]),
          Last10Draws = sum(Tie [1:10]),
          min_win = min(which(Wins1 == 1)),
          min_losses = min(which(Losses1 == 1)),
          min_draws = min(which(Tie == 1)),
          length = min(c(min_win, min_losses, min_draws)[c(min_win, min_losses, min_draws) > 1]))%>%
    
#Using the above variables to calculate PPM, PTPCT, GSM, and GAM and creating columns for each new variable
  #Points per match = Points divided by MatchesPlayed
mutate(PPM = Points / MatchesPlayed,
       #Point percentage calculated by dividing Points by 3 X MatchesPlayed
      PtPct = Points / (3 * MatchesPlayed),
      #Goals scored per match is Goals Scored (GS) divided by MatchesPlayed
      GSM = GS / MatchesPlayed,
      #Goals allowed per match is Goals Allowed (GA) divided by MatchesPlayed
      GAM = GA / MatchesPlayed,
#Create Record Column
Record = str_c(Wins, "-",Losses, "-", Draws),
#Create HomeRec Column for the home team's record
HomeRec = str_c(HomeWins, "-", HomeLosses, "-", HomeDraws),
#Create AwayRec Column for the away team's record
AwayRec = str_c(AwayWins, "-", AwayLosses, "-", AwayDraws),
#Last 10 Games Played for each team to find the last 10 wins, losses, draws; (W, L, D)
Last10 = str_c(Last10Wins, "-", Last10Losses, "-", Last10Draws),
#Find the latest Streak
streak_type = ifelse(min_win == 1, 'W', 'D'),
streak_type = ifelse(min_losses == 1, 'L', streak_type),
Streak = str_c(streak_type, length))%>%

#Return only the needed columns
select(Team,Record,HomeRec,AwayRec,MatchesPlayed,Points,PPM,PtPct,GS,GSM,GA,GAM,Last10,Streak)%>%ungroup

return(as.data.frame(team_data))
}


EPL_Standings("5/31/2018","2018/19")

I think you are missing a %>% after the above line. That's why it says you are applying select function to a Date function and not the Date column in the dataframe you just read via read.csv().

And identical error message?

Yes, it gave me the same exact message.

I made the minimal edits , i.e. to library() and the EPL_data missing %>% and did not receive an error ,rather there was some output and some warnings.


library(tidyverse)
library(lubridate)
EPL_Standings <- function(date, season){
  
  options(dplyr.summarise.inform = FALSE)
  
  #Data Frames
  url = paste("http://www.football-data.co.uk/mmz4281/",gsub("/","",substr(season, 3,7)),"/E0.csv",sep="")
  EPL_data = read.csv(url) %>% 
  
  #Select the columns needed for assignment
  select(Date,HomeTeam,AwayTeam,FTHG,FTAG,FTR)%>%
    
    #Use lubridate to change the date to read Month/Day/Year
    mutate(Date = dmy(Date))%>%
    filter(Date <= mdy(date))
  
  #Make a column called 'Team'
  Home <- EPL_data%>%
    select(Date,Team = HomeTeam,FTHG,FTAG,FTR)%>%
    mutate(HomeAway = 'Home')
  Away <- EPL_data%>%
    select(Date,Team = AwayTeam,FTHG,FTAG,FTR)%>%
    mutate(HomeAway = 'Away')
  #Join these sets
  team_data <- bind_rows(Home, Away)%>%
    arrange(Team, desc(Date))%>%
    #Check to see if the team won or lost
    mutate(Home_Win = ifelse (HomeAway == 'Home' & FTHG > FTAG, 1, 0), 
           Away_Win = ifelse (HomeAway == 'Away' & FTAG > FTHG, 1, 0),
           Home_Tie = ifelse (HomeAway == 'Home' & FTHG == FTAG, 1, 0),
           Away_Tie = ifelse (HomeAway == 'Away' & FTAG == FTHG, 1, 0),
           Home_Loss = ifelse (HomeAway == 'Home' & FTHG < FTAG, 1, 0),
           Away_Loss = ifelse (HomeAway == 'Away' & FTAG < FTHG, 1, 0),
           #Goals Scored for Home and Away
           Goals_Scored = ifelse (HomeAway == 'Home', FTHG, FTAG),
           #Goals Allowed = Goals by the away team
           Goals_Allowed = ifelse (HomeAway == 'Home', FTAG, FTHG),
           #Returns 1 for win, 0 for loss
           Wins1 = ifelse (Home_Win == 1 | Away_Win == 1, 1, 0),
           #Returns 1 for loss, 0 for win
           Losses1 = ifelse (Home_Loss == 1 | Away_Loss == 1, 1, 0),
           #Returns 1 if tie (Draw), 0 if not
           Tie = ifelse (Home_Tie == 1 | Away_Tie == 1, 1, 0),)%>%
    #Grouping by team
    group_by(Team)%>%
    
    #Using variables created above to calculate the following:
    #Wins is the sum of Wins1 created above
    summarize(Wins = sum(Wins1),
              #Ttl Losses is the sum of Losses1 created above
              Losses = sum(Losses1),
              #Ttl Draws is the sum of Tie(s) created above
              Draws = sum(Tie),
              #Ttl Wins by the Home team created above
              HomeWins = sum(Home_Win),
              #Ttl Losses by the Home Team created above
              HomeLosses = sum(Home_Loss),
              #Ttl Draws for the Home Team by using Home_Tie created above
              HomeDraws = sum(Home_Tie),
              #Ttl wins by the Away team from Away_Win created above
              AwayWins = sum(Away_Win),
              #Ttl Losses by the Away team from the Away_Loss created above
              AwayLosses = sum(Away_Loss),
              #Ttl Draws by the Away team from Away_tie created above
              AwayDraws = sum(Away_Tie),
              #Ttl Goals Scored for the season using Goals_Scored created above 
              GS = sum(Goals_Scored),
              #Ttl Goals Allowed (scored by the other team by using Goals_Allowed created above)
              GA = sum(Goals_Allowed),
              #Calculated Points by multiplying the wins by 3, and then adding the total ties (each worth 1 point)
              Points = (3 * sum(Wins1)) + sum(Tie),
              #Getting the matches played by each team
              MatchesPlayed = n(),
              #The following helps us get the team's record over the last 10 games
              Last10Wins = sum(Wins1 [1:10]),
              Last10Losses = sum(Losses1 [1:10]),
              Last10Draws = sum(Tie [1:10]),
              min_win = min(which(Wins1 == 1)),
              min_losses = min(which(Losses1 == 1)),
              min_draws = min(which(Tie == 1)),
              length = min(c(min_win, min_losses, min_draws)[c(min_win, min_losses, min_draws) > 1]))%>%
    
    #Using the above variables to calculate PPM, PTPCT, GSM, and GAM and creating columns for each new variable
    #Points per match = Points divided by MatchesPlayed
    mutate(PPM = Points / MatchesPlayed,
           #Point percentage calculated by dividing Points by 3 X MatchesPlayed
           PtPct = Points / (3 * MatchesPlayed),
           #Goals scored per match is Goals Scored (GS) divided by MatchesPlayed
           GSM = GS / MatchesPlayed,
           #Goals allowed per match is Goals Allowed (GA) divided by MatchesPlayed
           GAM = GA / MatchesPlayed,
           #Create Record Column
           Record = str_c(Wins, "-",Losses, "-", Draws),
           #Create HomeRec Column for the home team's record
           HomeRec = str_c(HomeWins, "-", HomeLosses, "-", HomeDraws),
           #Create AwayRec Column for the away team's record
           AwayRec = str_c(AwayWins, "-", AwayLosses, "-", AwayDraws),
           #Last 10 Games Played for each team to find the last 10 wins, losses, draws; (W, L, D)
           Last10 = str_c(Last10Wins, "-", Last10Losses, "-", Last10Draws),
           #Find the latest Streak
           streak_type = ifelse(min_win == 1, 'W', 'D'),
           streak_type = ifelse(min_losses == 1, 'L', streak_type),
           Streak = str_c(streak_type, length))%>%
    
    #Return only the needed columns
    select(Team,Record,HomeRec,AwayRec,MatchesPlayed,Points,PPM,PtPct,GS,GSM,GA,GAM,Last10,Streak)%>%ungroup
  
  return(as.data.frame(team_data))
}


EPL_Standings("5/31/2018","2018/19")
 [1] Team          Record        HomeRec       AwayRec       MatchesPlayed Points        PPM          
 [8] PtPct         GS            GSM           GA            GAM           Last10        Streak       
<0 rows> (or 0-length row.names)
Warning messages:
1: In min(which(Wins1 == 1)) :
  no non-missing arguments to min; returning Inf
2: In min(which(Losses1 == 1)) :
  no non-missing arguments to min; returning Inf
3: In min(which(Tie == 1)) :
  no non-missing arguments to min; returning Inf

I got it fixed! Thanks so much for taking the time to help! Much appreciated!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.

Just tried it, it did not work.