How can I automate data frame manipulation subbing in whatever team name input I have to return the manipulated version of that data? Rstudio

I'm trying to automate a bunch of data frame manipulations based on an input where it returns a one row dataframe called (team name input)stuff2022c. I will eventually turn this into a dataset for all teams in 2022 and possibly more seasons, which I would then just edit the function slightly to fix that. I am using R Studio.

datacleanup <- function(team){

##the teamcap2022 dataset is scraped and already named with (actual team name)cap2022
teamcap2022$Player <- gsub(" .*", "",  teamcap$Player) 
teamcap2022$Player <- gsub("\n\t\t\t\n", "", teamcap$Player)
teamcap2022$average <- gsub(",", "",  teamcap$average) 
teamcap2022$average <- gsub("\\$", "",  teamcap$average)
teamcap2022 <- subset(teamcap, select = c("Player", "POS", "average") )
teamcap2022$average <- as.numeric(teamcap$average)

teamcap2022 <- teamcap2022 %>%
  na.omit(teamcap2022) %>%
   mutate(posg = case_when(
     POS %in% c("T", "G", "C", "RT", "LT", "RG", "LG") ~ "OL",
     POS %in% c("QB") ~ "QB",
     POS %in% c("RB") ~ "RB",
     POS %in% c("WR") ~ "WR",
     POS %in% c("TE", "FB") ~ "TE",
     POS %in% c("K", "P", "LS") ~ "ST",
     POS %in% c("OLB", "ILB", "LB", "DE", "DT") ~ "Front7",
     POS %in% c("CB", "S", "DB", "FS", "SS", "NCB", "SAF") ~ "DB",
     TRUE ~ "Other"
            ),
     
          )
teamcap2022 <- teamcap2022 %>%
  group_by(posg) %>%
  mutate(cash = sum(average),
            nump = n()) %>%
  select(-c("POS", "average", "Player"))

teamcap2022 <- teamcap2022 %>%
  mutate(cap_pct = percent(cash/sum(cash)))




##the teampicks2022 for each team is already created outside this function already named with (actual team name)cap2022


teampicks2022 <- teampicks2022[c("pick", "posg")]
teampicks2022 <- teampicks2022 %>%
  left_join(thevaluechart, by = "pick") %>%
  mutate(
    top100 = case_when(
      pick < 101 ~ 1,
      TRUE ~ 0
    ),
    latepicks = case_when( 
      pick > 100 ~ 1,
      TRUE ~ 0
      )
  )




teampicks2022 <- teampicks2022 %>%
  group_by(posg) %>%
  summarize(
    top100 = sum(top100),
    latepicks = sum(latepicks),
    value = sum(value),
    
    .groups = 'drop'
  ) %>%
mutate(value_pct = percent(value/sum(value)))

team2022stuff <- teamcap2022 %>%
  left_join(teampicks2022, by = "posg")
  
  team2022stuffc <- team2022stuff %>%
  mutate(
    top100_QB = ifelse(posg == "QB", top100, NA),
    top100_RB = ifelse(posg == "RB", top100, NA),
    top100_WR = ifelse(posg == "WR", top100, NA),
    top100_OL = ifelse(posg == "OL", top100, NA),
    top100_TE = ifelse(posg == "TE", top100, NA),
    top100_ST = ifelse(posg == "ST", top100, NA),
    top100_DB = ifelse(posg == "DB", top100, NA),
    top100_Front7 = ifelse(posg == "Front7", top100, NA),
    latepicks_QB = ifelse(posg == "QB", latepicks, NA),
    latepicks_RB = ifelse(posg == "RB", latepicks, NA),
    latepicks_WR = ifelse(posg == "WR", latepicks, NA),
    latepicks_OL = ifelse(posg == "OL", latepicks, NA),
    latepicks_TE = ifelse(posg == "TE", latepicks, NA),
    latepicks_ST = ifelse(posg == "ST", latepicks, NA),
    latepicks_DB = ifelse(posg == "DB", latepicks, NA),
    latepicks_Front7 = ifelse(posg == "Front7", latepicks, NA),
    value_QB = ifelse(posg == "QB", value, NA),
    value_RB = ifelse(posg == "RB", value, NA),
    value_WR = ifelse(posg == "WR", value, NA),
    value_OL = ifelse(posg == "OL", value, NA),
    value_TE = ifelse(posg == "TE", value, NA),
    value_ST = ifelse(posg == "ST", value, NA),
    value_DB = ifelse(posg == "DB", value, NA),
    value_Front7 = ifelse(posg == "Front7", value, NA),
    cash_QB = ifelse(posg == "QB", cash, NA),
    cash_RB = ifelse(posg == "RB", cash, NA),
    cash_WR = ifelse(posg == "WR", cash, NA),
    cash_OL = ifelse(posg == "OL", cash, NA),
    cash_TE = ifelse(posg == "TE", cash, NA),
    cash_ST = ifelse(posg == "ST", cash, NA),
    cash_DB = ifelse(posg == "DB", cash, NA),
    cash_Front7 = ifelse(posg == "Front7", cash, NA),
    nump_QB = ifelse(posg == "QB", nump, NA),
    nump_RB = ifelse(posg == "RB", nump, NA),
    nump_WR = ifelse(posg == "WR", nump, NA),
    nump_OL = ifelse(posg == "OL", nump, NA),
    nump_TE = ifelse(posg == "TE", nump, NA),
    nump_ST = ifelse(posg == "ST", nump, NA),
    nump_DB = ifelse(posg == "DB", nump, NA),
    nump_Front7 = ifelse(posg == "Front7", nump, NA),
    cap_pct_QB = ifelse(posg == "QB", cap_pct, NA),
    cap_pct_RB = ifelse(posg == "RB", cap_pct, NA),
    cap_pct_WR = ifelse(posg == "WR", cap_pct, NA),
    cap_pct_OL = ifelse(posg == "OL", cap_pct, NA),
    cap_pct_TE = ifelse(posg == "TE", cap_pct, NA),
    cap_pct_ST = ifelse(posg == "ST", cap_pct, NA),
    cap_pct_DB = ifelse(posg == "DB", cap_pct, NA),
    cap_pct_Front7 = ifelse(posg == "Front7", cap_pct, NA),
    value_pct_QB = ifelse(posg == "QB", value_pct, NA),
    value_pct_RB = ifelse(posg == "RB", value_pct, NA),
    value_pct_WR = ifelse(posg == "WR", value_pct, NA),
    value_pct_OL = ifelse(posg == "OL", value_pct, NA),
    value_pct_TE = ifelse(posg == "TE", value_pct, NA),
    value_pct_ST = ifelse(posg == "ST", value_pct, NA),
    value_pct_DB = ifelse(posg == "DB", value_pct, NA),
    value_pct_Front7 = ifelse(posg == "Front7", cap_pct, NA)
  ) %>%
    ungroup() %>%
    select(-c("value", "top100", "latepicks", "posg", "cap_pct", "nump", "cash")) %>%
    summarise(
    across(starts_with("top100"), ~ last(na.omit(.))),
    across(starts_with("latepicks"), ~ last(na.omit(.))),
    across(starts_with("cash"), ~ last(na.omit(.))),
    across(starts_with("cap_pct"), ~ last(na.omit(.))),
    across(starts_with("nump"), ~ last(na.omit(.))),
    across(starts_with("value"), ~ last(na.omit(.)))
  ) %>%
  mutate_all(~ ifelse(is.na(.), 0, .))

}

I get Error in datacleanup(cardinals) : object 'teamcap' not found.

I have tried some versions of turning it to text to gsub then rerunning the function and I haven't had success. Any help on this matter would be extremely appreciated and I would be happy to share any results with anyone who wanted it there after.

Eyeballing the code I see only teamcap2022, not this. Where does it come into namespace?

This topic was automatically closed 21 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.