1

I have a list, where I have the optimal models ordered by some criteria. I would like to get the name of the variable. For example, for the index [1,1], I get the following:

print(opt_dos_variables_2[1,1])
c...Intercept.....ICI....LC.. 
                        "ICI"  

I just want the string between " ", i.e., the ICI.

What should I modify?

Thanks!

1 Answers1

2

You could use sub for a base R option:

input <- "c...Intercept.....ICI....LC.. 
                    \"ICI\""
output <- sub("^.*\"(.*?)\".*$", "\\1", input)
output

[1] "ICI"

If you wanted to use this logic against an entire list, you could use lapply with the above call to sub as the inline function, e.g.

lapply(your_list, function(x) sub("^.*\"(.*?)\".*$", "\\1", x))
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360