I want to be able to remove or insert arbitrary substrings from an existing r variable. My current solution uses system(), but I’m sure there’s an easier and more elegant way:
> filename <- "remove_this_my_file.txt"
> (file <- system(paste("echo ", filename, "| sed 's/remove_this_\\(.*\\)/\\1/'",sep=""), intern=T))
[1] "my_file.txt"
By the way, substr() is no good, since the position of the substring might vary from filename to filename.
You can use regular expressions in R via the
grep,sub,regexpr, and similar commands. It sounds like you’d wantsuborgsub. These operations are vectorized, which comes in handy at times.If you’re used to Perl-style regular expressions, you’ll want to set the
,perl=TRUEoption.