Thursday, 5 September 2013

How to find the base part of a variable name that ends in I, J, or IJ

How to find the base part of a variable name that ends in I, J, or IJ

I want to cut off the end part of a vector of characters of variable
length that all end in either I, J, or IJ, but haven't quite got it right
yet:
Current attempt, using a simple case.
vars <- c("VARI", "VARJ", "VARIJ")
sapply(vars, function(v) {
m <- regexec("^(.*)(?:I|J|IJ)$", v)
regmatches(v, m)[[1]][2]
})
However, it doesn't work for the IJ case:
VARI VARJ VARIJ
"VAR" "VAR" "VARI"

No comments:

Post a Comment