1

I am using following code which extracts the text between quotation in R. It works fine, the only problem is that it does not save the extracted text in a variable. What am I missing here? Is there a better way of doing this?

var2 <- cat(sub('.*"(.*)".*', "\\1", var1))
var2
Anand
  • 363
  • 2
  • 13
  • 2
    Remove the `cat` function. `cat` returns NULL. See `?cat`. If you really want to print out the result, wrap the sucker in `(...)` like this: `(var2 <- sub('.*"(.*)".*', "\\1", var1))`. – lmo Oct 26 '17 at 12:36
  • That's what I initially thought but removing cat does not work as it did not extract text with the quotation. As soon I as I add cat it does extract the text between quotation. – Anand Oct 26 '17 at 12:38
  • I am skeptical of this claim as I have never experienced this. Please provide an example of var1 that reproduces your problem. – lmo Oct 26 '17 at 12:41
  • @lmo I will attach the example shortly, thanks! – Anand Oct 26 '17 at 12:45
  • @lmo value of var1 is extracted from a fifth matrix column and it shows as this two lines: V5 “a5 \ “lb1 : c2 c4 c1 \”” When I use sub it shows me value as two lines: V5 “lb1: c2 c4 c1” And when I use cat with sub it shows value as “lb1: c2 c4 c1” – Anand Oct 26 '17 at 12:57

1 Answers1

3

You need to use capture.output in this case.

var2 <- capture.output(cat(sub('.*"(.*)".*', "\\1", var1)))
var2

Look at this question.

Barbara
  • 1,118
  • 2
  • 11
  • 34