regex - Split string on [:punct:] except for underscore in R -
i have equation string variables in string equation variables in r workspace. replace each variable numeric value in r workspace. easy enough when variable names don't contain punctuation.
here simple example.
x <- 5 y <- 10 yy <- 15 z <- x*(y + yy) zaschar <- "z=x*(y+yy)" vars <- unlist(strsplit(zaschar, "[[:punct:]]")) notvars <- unlist(strsplit(zaschar, "[^[:punct:]]")) varsvalues <- sapply(vars[vars != ""], fun=function(aaa) get(aaa)) notvarsvalues <- notvars[notvars != ""] paste(paste0(varsvalues, notvarsvalues), collapse="") this yields "125=5*(10+15)", great.
however, love option use underscores in variable names can use "subscripts" variable names. using these strings in math mode in r markdown.
so need [:punct:] excludes _. tried using [\\+\\-\\*\\/\\(\\)\\=] rather [:punct:], approach couldn't split on minus sign. there way preserve _?
instead of [:punct:] use unicode character class \pp (shortcut \p{p}) , negation \pp that:
[^\\pp_] (it works perl=true option)
Comments
Post a Comment