0

I have the variable 'qualification' in a df with observations like this:

  • a bachelor’s degree and 6 years of experience. At least 3 years of experience in the implementation and management...
  • at least 3 years of professional experience with strong recommendations from previous supervisors. experience in procurement, office administration, logistics management, and event/meeting coordination.
  • a ba/s or equivalent in relevant field; ma/s preferred. minimum 5 years’ ngo...
  • bachelor's degree required.• 2 years of relevant work experience...

I would like to create the variable exp (year of experience) substracting the number of years form qualification as for the example above: exp: 6 3 5 2

How can this be done?

abima3
  • 3
  • 2
  • Please edit the question to show us the code for your latest attempt and where you got stuck. See also: [ask] and [help/on-topic]. – Timur Shtatland Jan 21 '21 at 18:57
  • 1
    It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. – MrFlick Jan 21 '21 at 19:00

1 Answers1

0

Use str_extract from stringr library:

library(stringr)
df <- data.frame(qualification = c("a bachelor's degree and 6 years of experience. At least 3 years of experience in the implementation and management...", "at least 3 years of professional experience with strong recommendations from previous supervisors. experience in procurement, office administration, logistics management, and event/meeting coordination.", "non-matching string"))      
df$exp <- as.numeric(str_extract(df$qualification, "(\\d+)(?=\\s+year)"))
print(df$exp)
## [1]  6  3 NA
Timur Shtatland
  • 12,024
  • 2
  • 30
  • 47