0

So I'm needing to rename a column in R and from that column I need to condense the column. For example in the initial data frame it would say "2017-18" and "2018-19" and I need it to condense to the first four digits, essentially cutting off the "-##" portion. I've attempted to use substr() and when I do it says that I'm having issues with it converting to characters or attempting to convert to a character.

data <- read_excel("nba.xlsx")
data1<- data %>%
  rename(year=season) %>%
  select(year)
data1 <- data1 + as.numeric(substr(year,1,4))

Above is my code that I currently and have tried rearranging and moving things around. Any help would be greatly appreciated. Thank you!

1 Answers1

0

Use str_replace:

df <- tibble(season = c("2017-18", "2018-19"))

df %>% mutate(year = str_replace(season, "-.*", ""))

# A tibble: 2 x 2
  season  year 
  <chr>   <chr>
1 2017-18 2017 
2 2018-19 2018 

Alternately, use str_sub:

str_sub(season, 1, 4)
andrew_reece
  • 20,390
  • 3
  • 33
  • 58