R 从2列文本到1列

R 从2列文本到1列,r,R,我正在尝试将我的2个文本列转换为1 我正在处理PDF格式的圣经文本(),我正在使用pdftools包提取: text <- pdf_text("NewKingJamesBible.pdf") > length(text) #Total number of pages of document [1] 1815 这在dput形式中是相同的: 16 Then they journeyed from Bethel; and when there was still some dista

我正在尝试将我的2个文本列转换为1

我正在处理PDF格式的圣经文本(),我正在使用
pdftools
包提取:

text <- pdf_text("NewKingJamesBible.pdf")

> length(text) #Total number of pages of document
[1] 1815 
这在
dput
形式中是相同的:

16 Then they journeyed from Bethel; and when there was still some distance to go to Ephrath, Rachel began to give birth and she [r]suffered severe labor.      
17 When she was in severe labor the midwife said to her, “Do not fear, for now you have another son.”    
18 It came about as her soul was departing (for she died), that she named him [s]Ben-oni; but his father called him [t]Benjamin.    
19 So Rachel died and was buried on the way to Ephrath (that is, Bethlehem).    
20 Jacob set up a pillar over her grave; that is the pillar of Rachel’s grave to this day.    
21 Then Israel journeyed on and pitched his tent beyond the [u]tower of [v]Eder.     
22 It came about while Israel was dwelling in that land, that Reuben went and lay with Bilhah his father’s concubine, and Israel heard of it.
The Sons of Israel
Now there were twelve sons of Jacob—     
23 the sons of Leah: Reuben, Jacob’s firstborn, then Simeon and Levi and   Judah and Issachar and Zebulun;     
24 the sons of Rachel: Joseph and Benjamin;     
25 and the sons of Bilhah, Rachel’s maid: Dan and Naphtali;     
26 and the sons of Zilpah, Leah’s maid: Gad and Asher. These are the sons of Jacob who were born to him in Paddan-aram.     
27 Jacob came to his father Isaac at Mamre of Kiriath-arba (that is, Hebron), where Abraham and Isaac had sojourned.      
28 Now the days of Isaac were one hundred and eighty years.      
29 Isaac breathed his last and died and was gathered to his people, an old man [w]of ripe age; and his sons Esau and Jacob buried him.     
Esau Moves
36 Now these are the records of the generations of Esau (that is, Edom).
2 Esau took his wives from the daughters of Canaan: Adah the daughter of Elon the Hittite, and Oholibamah the daughter of Anah and the granddaughter of Zibeon the Hivite;     
3 also Basemath, Ishmael’s daughter, the sister of Nebaioth.     
4 Adah bore Eliphaz to Esau, and Basemath bore Reuel,
5 and Oholibamah bore Jeush and Jalam and Korah. These are the sons of Esau who were born to him in the land of Canaan.     
6 Then Esau took his wives and his sons and his daughters and all [x]his household, and his livestock and all his cattle and all his goods which he had acquired in the land of Canaan, and went to another land away from his brother Jacob.
dput输出

"16\r\n  Then they journeyed from Bethel. And    These were the sons of Jacob who were\r\nwhen there was but a little distance to   born to him in Padan Aram.\r\ngo to Ephrath, Rachel labored in\r\nchildbirth, and she had hard labor.       27\r\n                                            Then Jacob came to his father Isaac at\r\n                                          Mamre, or Kirjath Arba (that is, Hebron),\r\n17\r\n  Now it came to pass, when she was in    where Abraham and Isaac had dwelt.\r\nhard labor, that the midwife said to her,\r\n\"Do not fear; you will have this son      28\r\n                                            Now the days of Isaac were one\r\nalso.\"                                    hundred and eighty years.\r\n18\r\n  And so it was, as her soul was          29\r\n                                            So Isaac breathed his last and died,\r\ndeparting (for she died), that she called and was gathered to his people, being\r\nhis name Ben-Oni; but his father called   old and full of days. And his sons Esau\r\nhim Benjamin.                             and Jacob buried him.\r\n19\r\n  So Rachel died and was buried on the\r\nway to Ephrath (that is, Bethlehem).      36Now this is the genealogy of Esau,\r\n20                                        who is Edom.\r\n  And Jacob set a pillar on her grave,\r\nwhich is the pillar of Rachel's grave to  2\r\nthis day.                                  Esau took his wives from the daughters\r\n                                          of Canaan: Adah the daughter of Elon\r\n21                                        the Hittite; Aholibamah the daughter of\r\n  Then Israel journeyed and pitched his   Anah, the daughter of Zibeon the Hivite;\r\ntent beyond the tower of Eder.\r\n                                          3\r\n22                                         and Basemath, Ishmael's daughter,\r\n  And it happened, when Israel dwelt in   sister of Nebajoth.\r\nthat land, that Reuben went and lay with\r\nBilhah his father's concubine; and Israel 4\r\nheard about it. Now the sons of Jacob      Now Adah bore Eliphaz to Esau, and\r\nwere twelve:                              Basemath bore Reuel.\r\n                                          5\r\n23\r\n  the sons of Leah were Reuben,            And Aholibamah bore Jeush, Jaalam,\r\nJacob's firstborn, and Simeon, Levi,      and Korah. These were the sons of\r\nJudah, Issachar, and Zebulun;             Esau who were born to him in the land\r\n                                          of Canaan.\r\n24\r\n  the sons of Rachel were Joseph and      6\r\nBenjamin;                                  Then Esau took his wives, his sons, his\r\n                                          daughters, and all the persons of his\r\n25                                        household, his cattle and all his animals,\r\n  the    sons    of   Bilhah,    Rachel's and all his goods which he had gained\r\nmaidservant, were Dan and Naphtali;       in the land of Canaan, and went to a\r\n26\r\n                                          country away from the presence of his\r\n  and the sons of Zilpah, Leah's          brother Jacob.\r\nmaidservant, were Gad and Asher.\r\n"
奖金:

我尝试使用以下方式下载PDF:

download.file("http://www.apuritansmind.com/wp-content/uploads/PDF/NewKingJamesBible.pdf",
              "new_king_james_bible.pdf")
文件大小为6.5 MB,但打开时,它是完全空白的。为什么?
我必须手动下载并从我的电脑上阅读,然后使用
pdftools

提取文本。以下方法对您有效吗

# split character string on line breaks
output.by.line <- strsplit(output, "\n")[[1]]

# consider everything up to the first 42 characters as column 1, everything after as column 2
output.by.line <- c(substring(output.by.line, 1, 42), # column 1
                    substring(output.by.line, 43))    # column 2

# remove leading / trailing whitespace
output.by.line <- trimws(output.by.line)

# remove blank lines
output.by.line <- output.by.line[nchar(output.by.line) > 0]

# preface each section number with \n to facilitate splitting
# (may require some manual check as not every section number appears to be in its own line)
output.by.line <- ifelse(nchar(output.by.line) <= 2 & 
                           !is.na(as.integer(output.by.line)),
                         paste0("\n", output.by.line),
                         output.by.line)

# join all lines together & split by section, dropping empty lines if any
output.by.section <- strsplit(paste(output.by.line, collapse = " "), "\n")[[1]]
output.by.section <- output.by.section[nchar(output.by.section) > 0]

# remove repeated white space inside each section, if any
output.by.section <- stringr::str_squish(output.by.section)
(注意:是的,以36开头的一行在这里没有被确定为新段落,因为它不是在它自己的一行。我不确定处理这个问题的最佳方式是什么。如果只是几页,进行一些手动检查+更正可能是合理的。否则,它将取决于整个文本的编号逻辑。),&可能值得一提。)


在文件下载部分,您可能希望尝试以下解决方案(即指定
mode=“wb”
作为
download.file
的参数之一)。

您是否可以使用
dput
函数添加可复制的示例?还可以添加预期的输出(我不知道你想对章节号
16
17
)做什么。@PoGibas添加了预期输出和dput输出。谢谢,这正是我所需要的。它工作得很好,因为每个右栏都有42个字符。我还需要分隔第36行(它表示一个新章节),但我在此发布了一个新问题。
download.file("http://www.apuritansmind.com/wp-content/uploads/PDF/NewKingJamesBible.pdf",
              "new_king_james_bible.pdf")
# split character string on line breaks
output.by.line <- strsplit(output, "\n")[[1]]

# consider everything up to the first 42 characters as column 1, everything after as column 2
output.by.line <- c(substring(output.by.line, 1, 42), # column 1
                    substring(output.by.line, 43))    # column 2

# remove leading / trailing whitespace
output.by.line <- trimws(output.by.line)

# remove blank lines
output.by.line <- output.by.line[nchar(output.by.line) > 0]

# preface each section number with \n to facilitate splitting
# (may require some manual check as not every section number appears to be in its own line)
output.by.line <- ifelse(nchar(output.by.line) <= 2 & 
                           !is.na(as.integer(output.by.line)),
                         paste0("\n", output.by.line),
                         output.by.line)

# join all lines together & split by section, dropping empty lines if any
output.by.section <- strsplit(paste(output.by.line, collapse = " "), "\n")[[1]]
output.by.section <- output.by.section[nchar(output.by.section) > 0]

# remove repeated white space inside each section, if any
output.by.section <- stringr::str_squish(output.by.section)
> output.by.section
 [1] "16 Then they journeyed from Bethel. And when there was but a little distance to go to Ephrath, Rachel labored in childbirth, and she had hard labor."                                                                                                      
 [2] "17 Now it came to pass, when she was in hard labor, that the midwife said to her, \"Do not fear; you will have this son also.\""                                                                                                                           
 [3] "18 And so it was, as her soul was departing (for she died), that she called his name Ben-Oni; but his father called him Benjamin."                                                                                                                         
 [4] "19 So Rachel died and was buried on the way to Ephrath (that is, Bethlehem)."                                                                                                                                                                              
 [5] "20 And Jacob set a pillar on her grave, which is the pillar of Rachel's grave to this day."                                                                                                                                                                
 [6] "21 Then Israel journeyed and pitched his tent beyond the tower of Eder."                                                                                                                                                                                   
 [7] "22 And it happened, when Israel dwelt in that land, that Reuben went and lay with Bilhah his father's concubine; and Israel heard about it. Now the sons of Jacob were twelve:"                                                                            
 [8] "23 the sons of Leah were Reuben, Jacob's firstborn, and Simeon, Levi, Judah, Issachar, and Zebulun;"                                                                                                                                                       
 [9] "24 the sons of Rachel were Joseph and Benjamin;"                                                                                                                                                                                                           
[10] "25 the sons of Bilhah, Rachel's maidservant, were Dan and Naphtali;"                                                                                                                                                                                       
[11] "26 and the sons of Zilpah, Leah's maidservant, were Gad and Asher. These were the sons of Jacob who were born to him in Padan Aram."                                                                                                                       
[12] "27 Then Jacob came to his father Isaac at Mamre, or Kirjath Arba (that is, Hebron), where Abraham and Isaac had dwelt."                                                                                                                                    
[13] "28 Now the days of Isaac were one hundred and eighty years."                                                                                                                                                                                               
[14] "29 So Isaac breathed his last and died, and was gathered to his people, being old and full of days. And his sons Esau and Jacob buried him. 36Now this is the genealogy of Esau, who is Edom."                                                             
[15] "2 Esau took his wives from the daughters of Canaan: Adah the daughter of Elon the Hittite; Aholibamah the daughter of Anah, the daughter of Zibeon the Hivite;"                                                                                            
[16] "3 and Basemath, Ishmael's daughter, sister of Nebajoth."                                                                                                                                                                                                   
[17] "4 Now Adah bore Eliphaz to Esau, and Basemath bore Reuel."                                                                                                                                                                                                 
[18] "5 And Aholibamah bore Jeush, Jaalam, and Korah. These were the sons of Esau who were born to him in the land of Canaan."                                                                                                                                   
[19] "6 Then Esau took his wives, his sons, his daughters, and all the persons of his household, his cattle and all his animals, and all his goods which he had gained in the land of Canaan, and went to a country away from the presence of his brother Jacob."