Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 201894

Splitting a string based on locating a varying code (with a similar format)

$
0
0

I uploaded a txt file in to R as follows: Election_Parties <- readr::read_lines("Election_Parties.txt") Let's say the following text was in the file:

P23-Andalusian Social Democratic Party (Partido Social-Demócrata Andaluz [PSDA])
P24-Andalusian Socialist Movement (Movimiento Socialista Andaluz [MSA])
P235-Andalusian Socialist Party-Andalucian Party (Partido Socialista Andalucista-Partido
Andalucista [PSA-PA])
P26-Andalusist Party (Partido Andalucista [PA])
P217-Andecha Astur (Andecha Astur [AA])

I would like to have all information about a party on one line, no matter how long it is. So:

P25-Andalusian Socialist Party-Andalucian Party (Partido Socialista Andalucista-Partido
Andalucista [PSA-PA])

Should become:

P25-Andalusian Socialist Party-Andalucian Party (Partido Socialista Andalucista-Partido Andalucista [PSA-PA])

I guess I should first put all the text together:

Election_Parties <- paste(Election_Parties, collapse="")

And then split it when it finds the combination P**- or P***-. How do I write this last part?

EDIT:

The actual data I would like to apply this to looks more like this:

BOLIVIA
P17-Nationalist Revolutionary Movement-Free Bolivia Movement (Movimiento 
Nacionalista Revolucionario [MNR])
P19-Liberty and Justice (Libertad y Justicia [LJ])
P20-Tupak Katari Revolutionary Movement (Movimiento Revolucionario Tupak Katari [MRTK])

COLOMBIA
P1-Democratic Aliance M-19 (Alianza Democratica M-19 [AD-M19])
P2-National Popular Alliance (Alianza Nacional Popular [ANAPO])
P3-Indigenous Authorities of Colombia (Autoridades Indígenas 
de Colombia)

Viewing all articles
Browse latest Browse all 201894

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>