You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用R提取Coldplay《Yellow》歌词指定词汇句子的编译问题求助

R语言提取《Yellow》歌词指定词汇句报错解决方案

核心问题原因

你创建字符向量报错,90%以上是歌词换行拆分错误、字符串和内部特殊符号冲突、语法漏写分隔符这三类问题,直接按以下步骤实现即可:

完整可运行代码

1. 正确构造歌词字符向量

每一句歌词单独作为向量的一个元素,统一用双引号包裹字符串,避免歌词内单引号引发的转义冲突:

# 存储《Yellow》歌词,每行对应原歌词1个句子
yellow_lyrics <- c(
  "Look at the stars",
  "Look how they shine for you",
  "And everything you do",
  "Yeah, they were all yellow",
  "I came along",
  "I wrote a song for you",
  "And all the things you do",
  "And it was called Yellow",
  "So, then I took my turn",
  "What a thing to've done",
  "And it was all yellow",
  "Your skin",
  "Oh yeah, your skin and bones",
  "Turn in to something beautiful",
  "Do you know",
  "You know I love you so",
  "You know I love you so",
  "I swam across",
  "I jumped across for you",
  "What a thing to do",
  "'Cause you were all yellow",
  "I drew a line"
)

2. 匹配指定词汇的句子

用正则匹配实现多关键词检索,支持忽略大小写:

# 定义待检索的目标词汇
target_keywords <- c("everything", "along", "wrote")
# 构造匹配规则
match_rule <- paste(target_keywords, collapse = "|")
# 提取匹配到的句子
result <- yellow_lyrics[grepl(match_rule, yellow_lyrics, ignore.case = TRUE)]

3. 输出结果

运行后打印result即可得到目标内容:

[1] "And everything you do"   "I came along"            "I wrote a song for you"

常见报错排查点

  • 字符向量的每个元素之间漏写逗号,R会判定为语法不完整无法运行
  • 用单引号包裹字符串时,歌词内的'Cause等带单引号的内容会和外层引号冲突,建议统一用双引号包裹歌词元素
  • 直接将整段未拆分的歌词粘贴进c()函数,会导致整个歌词仅生成1个向量元素,无法按句子匹配

内容的提问来源于stack exchange,提问作者ZackFaier

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.29 04:24:03