samedi 18 avril 2015

Splitting a string at at every element of a specified vector

I am wrestling with a problem for which I was sure there was an easy solution, but I haven't been able to find it. Thanks for any help.


I'm trying to split a string of text, whenever an element of a separate vector occurs. Something like the following:



fruits<-c("APPLE","BANANA","ORANGE")
string<-("This is a list of fruits and their properties.
APPLE This is a red fruit, typically very SWEET!
BANANA This is a yellow fruit, also sweet!
ORANGE This is an orange fruit and also, yes, sweet")


My desired output is a list/vector of 4 elements, each of which contains the splits of the strings before/after any of the elements of 'fruits' occurs. So, something like:



c("This is a list of fruits and their properties",
"APPLE This is a red fruit, typically very SWEET!",
"BANANA This is a yellow fruit, also sweet!,
"ORANGE This is an orange fruit and also, yes, sweet")


I have tried



strsplit(string,split=fruits)


As well as a couple of other things, but with no success. What I'm actually trying to do is split a .pdf codebook that I've converted into .txt by a list of words (countries), that correspond to sections of the codebook.


Thanks in advance!


Aucun commentaire:

Enregistrer un commentaire