Home > Back-end >  How do you change values in a column of a dataframe to a part of the current value?
How do you change values in a column of a dataframe to a part of the current value?

Time:11-13

I'm working on cleaning a dataframe. One of the columns is "Year" and contains values in the following format:

"2012-2013"

I would like to change the values in the column to just show the latter year:

"2013"

I have no idea of how to start. Could someone assist me please? Thank you!

CodePudding user response:

Here's a solution using tidyverse functions. str_sub takes a string and extracts the values between the starting and ending positions provided.

dat <- data.frame(year = c('2012-2013', '2013-2014'))
dat %>% mutate(new_year = str_sub(year, start = 6, end = 9))

CodePudding user response:

Try substring value from left to right in data frame column - like 6 to 9 digit value

v$new<-substr(v[,1] , 6,9)

2013
  • Related