跳过仅包含R

时间:2016-02-28 10:01:00

标签: html r dataframe

我有一个阅读一些html子网站的问题。他们中的大多数工作得很好但是例如http://www-history.mcs.st-andrews.ac.uk/Biographies/De_Morgan.html在H1和H3中有空行。因为我的data.frame是一个完全混乱的人,例如: data frame example。框架包含4列“名称”“出生日期和地点”“日期和地点”“链接”。我想在LaTeX中制作一张桌子,但由于那些带有空格的行,我的标签在某些点上的方向错误而且一个人的名字是他的出生日期等等。使用简单地使用从j = 1到长度的循环(LinkiWlasciwy)来读取那些网站

matematyk=LinkWlasciwy[j] %>% read_html() %>% html_nodes(selektor1) %>% html_text()

其中selektor1 =“h3 font,h1”。之后我保存它包含到.txt文件并在另一个脚本中读取它我应该根据这些数据制作.tex文件。在我看来,最好只删除文件中只包含空格的行,例如空格,\ n等。在我的txt文件中,例如。

  

Marie-Sophie Germain | 1776年4月1日

     

在法国巴黎| 1831年6月27日

     

在法国巴黎| www-history.mcs.st-andrews.ac.uk/Biographies/Germain.html |

作为分隔符我使用“|”。并非所有这些都是相同的,有些只包含一个空格,有些只有两个等等。我只想把每一个错误的记录带到这个

  

Marie-Sophie Germain | 1776年4月1日在法国巴黎| 1831年6月27日在法国巴黎| www-history.mcs.st-andrews.ac.uk/Biographies/Germain.html |

我不得不从文本示例中删除http://,因为我还没有10个声望而且它们被视为链接

1 个答案:

答案 0 :(得分:0)

您可以使用库stringi

library(stringi)
line<-c("Marie-Sophie Germain| 1 April 1776",
" ",
"in Paris, France| 27 June 1831",
"   ",
"in Paris, France|www-history.mcs.st-andrews.ac.uk/Biographies/Germain.html|")

line2<- line[stri_count_regex(line, "^[ \\t]+$") ==0]
line2
stri_paste(line2, collapse="")

结果:

[1] "Marie-Sophie Germain| 1 April 1776in Paris, France| 27 June 1831in Paris, France|www-history.mcs.st-andrews.ac.uk/Biographies/Germain.html|"
相关问题