0

I have large list in R with more than 5000 elements. The elements are of the form:

$`/home/ricardo/MultiClass/data//Execucao_PUBLICACAO_DECISAO_INTERLOCUTORIA_DETERMINACAO_DE_PAGAMENTO/1117.txt.V1 

[1] DATA DE DISPONIBILIZACAO DA PUBLICACAO PELA FONTE OFICIAL: 16/11/2016 Pag 4279 Decisao Processo N RTOrd-0122200-90.2006.5.15.0087  <truncated>`

I would like to transform this in a two columns dataframe where:

c1       
The contents between $ and [1]

c2    
rest of the text

How can I do this split? Important to note that the numberof strings between $ and [1] can change, and the strings $, [ e ] can appear in the rest of the text.

Thanks in advance, Ricardo.

Daniel
  • 1,202
  • 2
  • 16
  • 25
  • Welcome to StackOverflow. Please take a look at these tips on how to produce a [minimum, complete, and verifiable example](http://stackoverflow.com/help/mcve), as well as this post on [creating a great example in R](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). Perhaps the following tips on [asking a good question](http://stackoverflow.com/help/how-to-ask) may also be worth a read. – lmo Feb 06 '17 at 21:13

2 Answers2

1
library(stringr)

string <- '$/home/ricardo/MultiClass/data//Execucao_PUBLICACAO_DECISAO_INTERLOCUTORIA_DETERMINACAO_DE_PAGAMENTO/1117.txt.V1 [1] DATA DE DISPONIBILIZACAO DA PUBLICACAO PELA FONTE OFICIAL: 16/11/2016 Pag 4279 Decisao Processo N RTOrd-0122200-90.2006.5.15.0087'

c1 <- str_match(string = string, pattern = "^\\$(.*) \\[1\\] (.*)")[,2]
c2 <- str_match(string = string, pattern = "^\\$(.*) \\[1\\] (.*)")[,3]
salient
  • 2,316
  • 6
  • 28
  • 43
1

The $ ... text is the name of the list element, and the [1] ... is the value of that element. You can extract these (or better yet, assign them correctly when reading in your data).

a <- list(`this is the name` = "data stored in that variable")

a
#> $`this is the name`
#> [1] "data stored in that variable"

names(a)
#> [1] "this is the name"

as.character(a)
#> [1] "data stored in that variable"
Jonathan Carroll
  • 3,897
  • 14
  • 34