【问题标题】:add text to atomic (character) vector in r将文本添加到 r 中的原子(字符)向量
【发布时间】:2020-03-04 16:55:45
【问题描述】:

下午好,我不是原子向量方面的专家,但我想了解一下它

我有电影“Coco”的剧本,我希望能够获得以 1.、2.、... 形式编号的行(整部电影共有 130 个场景)。我想将电影的每个场景的行转换为包含“场景1”,“场景2”,直到“场景130”的一行并依次实现。

url <- "https://www.imsdb.com/scripts/Coco.html"

coco <- read_lines("coco2.txt") #after clean 
class(coco)
typeof(coco)

"                                                                        48."      
 [782] "     arms full of offerings."                                                     
 [783] "      Once the family clears, Miguel is nowhere to be seen."                      
 [784] "      INT. NEARBY CORRIDOR"                                                       
 [785] "     Miguel and Dante hide from the patrolman.     But Dante wanders"             
 [786] "     off to inspect a side room."                                                 
 [787] "      INT. DEPARTMENT OF CORRECTIONS"                                             
 [788] "     Miguel catches up to Dante.      He overhears an exchange in a"              
 [789] "     nearby cubicle."                                                             

 [797] "                                                          49."                    
 [798] "                 And amigos, they help their amigos."                             
 [799] "                 worth your while."                                               
 [800] "     workstation."                                                                
 [801] "      Miguel perks at the mention of de la Cruz."                                 


 [809] "      Miguel follows him."                                                        
 [810] "                                                                     50." # Its scene number     
 [811] "      INT. HALLWAY"      


s <- grep(coco, pattern = "[^Level].[0-9].$", value = TRUE)

我的解决方案是错误的,因为它不是顺序的

v <- gsub(s, pattern = "[^Level].[0-9].$", replacement = paste("Scene", sequence(1:130)))


[1] "                                                                   Scene1"          
  [2] "                                                                   Scene1"          
  [3] "                                                                  Scene1"           
  [4] "                                                                       Scene1"      
  [5] "                                                                    Scene1"         
  [6] "                                                                   Scene1"          

【问题讨论】:

  • 您试图用grep(coco, pattern = "[^Level].[0-9].$", value = TRUE)查找文本中的哪些字符串
  • library(readr) coco imsdb.com/scripts/Coco.html", skip = 238, skip_empty_rows = TRUE, locale = default_locale()) coco ]+>", "", coco) coco
  • 请。运行前面的代码,我可以找到为什么我使用 [^ level] 在我的 r 脚本中不包含 level 的原因

标签: r text character readr tidytext


【解决方案1】:

我不清楚[^Level] 代表什么。但是,如果文本中行末的数字代表场景编号,则可以使用 ( ) 捕获数字并将其替换为替换文本,如下所示:

 v <- gsub(s, pattern = " ([0-9]{1,3})\\.$", replacement = "Scene \\1")

【讨论】:

  • 谢谢,谢谢!!这是我从昨天开始一直在寻找的东西,但我在互联网上没有找到它。有效!
猜你喜欢
  • 2015-05-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-04-27
  • 1970-01-01
  • 1970-01-01
  • 2021-07-02
相关资源
最近更新 更多