jeudi 10 décembre 2015

How to add an attribute to grep () Output

What I'm trying to do is following: use a grep() function to search for a pattern (a list of numbers, which I called "toMatch") in a data.frame ("News"). So, what I want it to do is search for those numbers in the news and return the matches (in the form "number", "corresponding news"). Unfortunately, I could so far only get a list of the corresponding news as a result. Any idea how I can add an attribute with the corresponding number from the match to the output? (in a way create a key-value pairs as an output)

Here a simple short example of my code:

News <- c ("AT000000STR2 is schwierig", "AT", "ATI", "AT000000STR1")
toMatch <- c("AT000000STR1","AT000000STR2","DE000000STR1","DE000000STR2")
matches <- unique (grep(paste(toMatch,collapse="|"),News, value=TRUE))
matches

And here the result:

> matches
[1] "AT000000STR2 is schwierig" "AT000000STR1" `

What I would like to have is a list or better yet Excel file, looking like this:

 AT000000STR2 "AT000000STR2 is schwierig" 
 AT000000STR1 "AT000000STR1"

Help is much appreciated.

Aucun commentaire:

Enregistrer un commentaire