Ben Young Ben Young - 3 months ago 13
R Question

R split text on empty line

I have a very long file that looks like this :

"Ach! Hans, Run!"
2RRGG
Enchantment
At the beginning of your upkeep, you may say "Ach! Hans, run! It's the . . ." and name a creature card. If you do, search your library for the named card, put it into play, then shuffle your library. That creature has haste. Remove it from the game at end of turn.
UNH-R

A Display of My Dark Power
Scheme
When you set this scheme in motion, until your next turn, whenever a player taps a land for mana, that player adds one mana to his or her mana pool of any type that land produced.
ARC-C

AErathi Berserker
2RRR
Creature -- Human Berserker
2/4
Rampage 3 (Whenever this creature becomes blocked, it gets +3/+3 until end of turn for each creature blocking it beyond the first.)
LE-U

AEther Adept
1UU
Creature -- Human Wizard
2/2
When AEther Adept enters the battlefield, return target creature to its owner's hand.
M11-C, M12-C, DDM-C

...


I'd like to load this file into a data.frame or vector "oracle", split by each empty line(actually a space and a newline) so that

oracle[1]


gives output like

"Ach! Hans, Run!" 2RRGG Enchantment At the beginning of your upkeep, you may say "Ach! Hans, run! It's the . . ." and name a creature card. If you do, search your library for the named card, put it into play, then shuffle your library. That creature has haste. Remove it from the game at end of turn. UNH-R


I've tried code like

oracle <- read.table(file = "All Sets.txt", quote = "", sep="\n")


as well as scan(), but

oracle[1]


gives very long, undesired output.

Thanks!

Answer

Try this, based on your edited question:

oracle <- readLines("BenYoung.txt")

breaks <- which(! nzchar(oracle))
nbreaks <- length(breaks)
if (nbreaks > 0L) {
  oracle <- mapply(function(a,b) paste(oracle[a:b], collapse = " "),
                   c(1L, breaks[-nbreaks]),
                   c(breaks[-1L], length(oracle)))
}

oracle[1]
# [1] "\"Ach! Hans, Run!\"  2RRGG Enchantment At the beginning of your upkeep, you may say \"Ach! Hans, run! It's the . . .\" and name a creature card. If you do, search your library for the named card, put it into play, then shuffle your library. That creature has haste. Remove it from the game at end of turn. UNH-R  A Display of My Dark Power Scheme When you set this scheme in motion, until your next turn, whenever a player taps a land for mana, that player adds one mana to his or her mana pool of any type that land produced. ARC-C "

Edit: though this works fine if you always have truly-empty lines as breaks, you can use this line instead to use lines with white-space only:

breaks <- which(grepl("^[[:space:]]*$", oracle))

This gives the same results when the lines are truly empty.

Comments