R 在空行上分割文本

2023-12-08

我有一个很长的文件,如下所示:

"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

...

我想将此文件加载到 data.frame 或向量“oracle”中,按每个空行(实际上是一个空格和一个换行符)分割,以便

oracle[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

我试过类似的代码

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

以及 scan(),但是

oracle[1]

给出很长的、不需要的输出。

Thanks!


根据您编辑的问题尝试一下:

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


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"

编辑:虽然如果您始终将真正的空行作为中断,则效果很好,但您可以使用此行来代替仅使用空白行:

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

当行真正为空时,这会给出相同的结果。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

R 在空行上分割文本 的相关文章

  • 如何在 R 中“推断”面板数据的值?

    我有一个带有 NA 值的面板数据 如下所示 uid year month day value 1 1 2016 8 1 NA 2 1 2016 8 2 NA 3 1 2016 8 3 30 4 1 2016 8 4 NA 5 1 2016
  • 安装 gplots 时出错

    我正在 OSX v 10 9 2 上运行 R v 3 0 3 当尝试使用以下命令在 R studio 中安装 gplots 包时 出现错误 gt library gplots Error in library gplots there is
  • r:按多列分组并计数

    我有以下数据框 df LeftOrRight SpeedCategory NumThruLanes R 25to45 3 L 45to62 2 R Gt62 1 我想按速度类别对其进行分组 并循环遍历其他列以获取每个速度类别中每个唯一代码的
  • 如何上下移动字符串并从中删除相似的字符串?

    我问了一个可能不太清楚的问题 所以我尝试用一 种可以理解的方式来解释它 这是我的数据 我的数据看起来像这样 看起来像这样 V1 V2 V3 1 Q9UNZ5 Q9Y2W1 2 Q9ULV4 Q6QEF8 3 Q9UNZ5 4 Q9H6F5
  • 如何更改 r 中的树状图标签

    我在 R 中有一个树形图 它基于使用 hclust 的分层聚类 我正在对不同颜色的标签进行着色 但是当我尝试使用以下命令更改树图的标签 集群所基于的数据帧的行 时dendrogram dendrogram gt set labels dat
  • 配置 fix() 和 edit() 以从 R/RStudio 在 Notepad++ 中打开

    当我在 RStudio 或 RGUI 中执行此操作时 fix SomeFunction 或使用edit 我可以在记事本中看到该函数的代码 有什么方法可以更改此设置 以便代码预览在 Notepad 中打开 而不是在普通的旧记事本中打开 同样
  • 如何使用 ggplot2 对曲线下的区域进行着色

    我一直在尝试使用 ggplot2 生成类似于此 R 图形的绘图 xv lt seq 0 4 0 01 yv lt dnorm xv 2 0 5 plot xv yv type l polygon c xv xv lt 1 5 1 5 c y
  • 在 R 中使用深度网络和 MNIST 数据读取手写数字第 3 部分

    我尝试编写一个基于深度网络的程序来读取手写数字 我在 Youtube 上找到了一个代码 https www youtube com watch v 5bso 5X7Zu4 https www youtube com watch v 5bso
  • 使用 2 个向量参数翻转函数

    我想对需要 2 个向量参数的函数应用滚动 这是使用 data table 的示例 不起作用 library data table df lt as data table cbind data frame x 1 100 y 101 200
  • data.table 的包装函数

    我有一个已经使用 data frame 上下文编写的项目 为了缩短计算时间 我尝试利用 data table 的速度 我的方法是构造包装函数 读取帧 将它们转换为表 进行计算 然后转换回帧 这是一个简单的例子 FastAgg lt func
  • R中使用余弦距离的层次聚类

    我想通过使用余弦相似度与 R 编程语言对文档语料库进行层次聚类 但出现以下错误 if is na n n gt 65536L stop 大小不能为 NA 或 超过 65536 需要 TRUE FALSE 时缺少值 我应该怎么办 为了重现它
  • 在 R 的替换命令中取消引用字符串

    我想知道是否可以unquote通过替换命令传递给表达式的字符串 具体来说 我使用 dplyr 从数据框中过滤和选择 gt w subject sex response 1 1 M 19 08 2 2 M 16 46 6 6 M 23 60
  • 如何不显示 ggplot 轴上的所有标签?

    I m trying to using ggplot2 to plot this But as you can see on the x axis you can t read anything 那么如何在 x 轴上显示每 10 年的值呢
  • R 无法回忆起内存中的对象

    我正在构建一个包含多个步骤的函数 其中每个步骤都会创建一个对象 某个步骤失败 temp3 并且无法找到前面的步骤对象 错误 未找到对象 temp2 我不知道为什么 我有类似的函数 遵循完全相同的结构 每个步骤都遵循先前创建的对象 在函数内
  • 连接树状图和热图

    我有一个heatmap 一组样本的基因表达 set seed 10 mat lt matrix rnorm 24 10 mean 1 sd 2 nrow 24 ncol 10 dimnames list paste g 1 24 sep p
  • R 获取子字符串和正则表达式?

    我有一组文件名字符串 我想提取 符号之后但文件扩展名之前的所有字符 例如 文件名之一是 HelloWorld you txt 我想返回字符串you 这是我的代码 hashPos grep name fixed TRUE dotPos len
  • ggplot2 + 使用比例 X 的日期结构

    我真的需要帮助 因为我已经迷路了 我正在尝试创建一个折线图 显示几个团队一年来的表现 我将一年分为几个季度 2012 年 1 月 1 日 2012 年 4 月 1 日 2012 年 8 月 1 日 12 1 12 并将 csv 数据帧加载到
  • Matlab 中是否有相当于 R 的 dput() 的函数?

    Matlab 中是否有相当于 R 的 dput 的函数 dput 将 R 对象的 ASCII 文本表示形式写入文件或连接 UPDATE 1 添加了递归和对单元格的支持 UPDATE 2 添加了对结构的支持 UPDATE 3 增加了对逻辑 整
  • ggplot 按因子和梯度颜色

    我正在尝试绘制一个对两个变量 一个因子和一个强度 进行着色的图 我希望每个因素都是不同的颜色 并且我希望强度是白色和该颜色之间的渐变 到目前为止 我已经使用了诸如对因子进行分面等技术 将颜色设置为两个变量之间的相互作用 并将颜色设置为因子并
  • 解压 R 数据框中的列表

    我有一个dataframe其中一个字段包含不同长度的列表 我想将该字段中列表的每个元素提取到其自己的字段中 以便我可以将结果收集到一个很长的字段中dataframe每个列表元素都有一个 id 这是一个例子dataframe dat lt s

随机推荐