Okay - maybe this is a better example. I am looking for guidance/references on how to reference a variable within a regex - not how to build a regex for this data.
Try using the paste0()
function. That will put together all your variables and any regular expressions you want to use.
grep(paste0("^.*", variable, ".*$"), d1)
you can also add the argument collapse = ""
to paste0()
if your variable could have >1 element
You'll need to escape various characters to use variables in regex, but why not do the simpler thing?
sub('(.*)ICA.*', '\\1', d1)
#[1] "CCA: 135 cm/sec " "CCA: 150 cm/sec "
sub('.*(ICA.*)', '\\1', d1)
#[1] "ICA: 50 cm/sec" "ICA: 75 cm/sec"
Try this:
> d1 <- c("CCA: 135 cm/sec ICA: 50 cm/sec", "CCA: 150 cm/sec ICA: 75 cm/sec")
> t(strapplyc(d1, "\\w+: \\S+ \\S+", simplify = TRUE))
[,1] [,2]
[1,] "CCA: 135 cm/sec" "ICA: 50 cm/sec"
[2,] "CCA: 150 cm/sec" "ICA: 75 cm/sec"