将2个数字字符拆分为自己的新行

时间:2019-04-20 11:48:04

标签: r

我有一个包含84个元素的角色对象。

 > head(output.by.line)
[1] "\n17"                                  
[2] "Now when Joseph saw that his father"   
[3] "laid his right hand on the head of"    
[4] "Ephraim, it displeased him; so he took"
[5] "hold of his father's hand to remove it"
[6] "from Ephraim's head to Manasseh's"

但是有一行有2个数字(49)不在其自己的行中:

  

[35] "49And Jacob called his sons and"

我想将其转换为:

[35] "\n49" 
[36] "And Jacob called his sons and"

并以正确的数字插入对象34之后。

输出输出

dput(output.by.line)
c("\n17", "Now when Joseph saw that his father", "laid his right hand on the head of", 
"Ephraim, it displeased him; so he took", "hold of his father's hand to remove it", 
"from Ephraim's head to Manasseh's", "head.", "\n18", "And Joseph said to his father, \"Not so,", 
"my father, for this one is the firstborn;", "put your right hand on his head.\"", 
"\n19", "But his father refused and said, \"I", "know, my son, I know. He also shall", 
"become a people, and he also shall be", "great; but truly his younger brother shall", 
"be greater than he, and his descendants", "shall become a multitude of nations.\"", 
"\n20", "So he blessed them that day, saying,", "\"By you Israel will bless, saying, \"May", 
"God make you as Ephraim and as", "Manasseh!\"' And thus he set Ephraim", 
"before Manasseh.", "\n21", "Then Israel said to Joseph, \"Behold, I", 
"am dying, but God will be with you and", "bring you back to the land of your", 
"fathers.", "\n22", "Moreover I have given to you one", "portion above your brothers, which I", 
"took from the hand of the Amorite with", "my sword and my bow.\"", 
"49And Jacob called his sons and", "said, \"Gather together, that I may tell", 
"you what shall befall you in the last", "days:", "\n2", "\"Gather together and hear, you sons of", 
"Jacob, And listen to Israel your father.", "\n3", "\"Reuben, you are my firstborn, My", 
"might and the beginning of my strength,", "The excellency of dignity and the", 
"excellency of power.", "\n4", "Unstable as water, you shall not excel,", 
"Because you went up to your father's", "bed; Then you defiled it-- He went up to", 
"my couch.", "\n5", "\"Simeon and Levi are brothers;", "Instruments of cruelty are in their", 
"dwelling place.", "\n6", "Let not my soul enter their council; Let", 
"not my honor be united to their", "assembly; For in their anger they slew a", 
"man, And in their self-will they", "hamstrung an ox.", "\n7", 
"Cursed be their anger, for it is fierce;", "And their wrath, for it is cruel! I will", 
"divide them in Jacob And scatter them", "in Israel.", "\n8", 
"\"Judah, you are he whom your brothers", "shall praise; Your hand shall be on the", 
"neck of your enemies; Your father's", "children shall bow down before you.", 
"\n9", "Judah is a lion's whelp; From the prey,", "my son, you have gone up. He bows", 
"down, he lies down as a lion; And as a", "lion, who shall rouse him?", 
"\n10", "The scepter shall not depart from", "Judah, Nor a lawgiver from between his", 
"feet, Until Shiloh comes; And to Him", "shall be the obedience of the people.", 
"\n11", "Binding his donkey to the vine, And his", "donkey's colt to the choice vine, He"
)

3 个答案:

答案 0 :(得分:3)

请检查以下内容:

library(tidyverse)

split_line_number <- function(x) {
    x %>%
      str_replace("^([0-9]+)", "\n\\1\b") %>% 
      str_split("\b")
}


output.by.line %>% 
  map(split_line_number) %>% 
  unlist()

# Output:
# [35] "\n49"                                      
# [36] "And Jacob called his sons and"             
# [37] "said, \"Gather together, that I may tell"  
# [38] "you what shall befall you in the last"  

答案 1 :(得分:1)

使用stringr::str_match的选项是匹配一个可选数字的两个组成部分,后跟所有内容。从匹配的矩阵(2:3)获取捕获的输出,并通过删除NA和空字符串来创建新的字符串矢量。

vals <- c(t(stringr::str_match(output.by.line, "(\n?\\d+)?(.*)")[, 2:3]))
output <- vals[!is.na(vals) & vals != ""]

output[32:39]
#[1] "portion above your brothers, which I"
#[2] "took from the hand of the Amorite with"
#[3] "my sword and my bow.\""                 
#[4] "49" 
#[5] "And Jacob called his sons and"            
#[6] "said, \"Gather together, that I may tell"
#[7] "you what shall befall you in the last"    "days:"                            

答案 2 :(得分:1)

我们将使用stringer包:

 removeQuestion(index) {
   this.setState(({ comps }) => ({ comps: comps.filter((_, i) => i !== index) }));
 }

修改对象:

library(stringr)

打印结果:

output.by.line <- unlist(
  ifelse(grepl('[[:digit:]][[:alpha:]]', output.by.line), str_split(gsub('([[:digit:]]+)([[:alpha:]])', paste0('\n', '\\1 \\2'), output.by.line), '[[:blank:]]', n = 2), output.by.line)
)