In the below data frame, I am trying to build a new column "avcomp" which extracts the numeric value between the whitespace and the % sign from the column "code", if the duty_nature is "M" or "C". I tried the below code but it only manages to get the first two numbers and the percent sign. Can you please help?
The avcomp column should read
>[1] "30", "0.50, ""
Thank you!
code <- c("Greater than 30% of something","Less than 0.50% of something","30%")
duty_nature<- c("M","C","A")
test <-data.frame(code,duty_nature)
test$avcomp <- ifelse(test$duty_nature == "M" | test$duty_nature == "C",str_sub(str_match(test$code,"\\s*(.*?)%\\s*"),-4,-1),"")