【问题标题】:I need to filter data without missing information, it is character but I can't filter it我需要过滤数据而不丢失信息,它是字符但我无法过滤它
【发布时间】:2021-10-24 08:13:56
【问题描述】:

库(XML) 图书馆(dplyr) 图书馆(rvest)

presid https://en.wikipedia.org/wiki/List_of_presidents_of_Peru") %>% # 读取html页面

 html_nodes("table") %>% # extract nodes which contain a table
  .[3] %>% # select the node which contains the relevant table
  html_table(header = NA,
             trim = T) # extract the table

t3 <- presid[[1]] # flatten data

t4 <-t3[unique(t3$N),] # eliminated duplicate
 
t5 <- subset(t4,!is.na(President))# 

我需要阅读此表并以在过滤数据时不允许丢失大量信息的最佳方式过滤数据。 行的丢失非常重要,它从t3的98行减少到t4的72行和t5的63行,而实际上我只需要将信息从98行减少到84行即可通过N列过滤

我试过这些公式,但没有结果

strsplit (as.character (t3$N), split = "(? <= [a-zA-Z]) (? = [0-9])", perl = TRUE)

其他

grep("[[:numeric:]]{2, }",N,value=T)

我需要过滤的第 N 列的行是小数点为 0.5、2.5、6.5、6.6 的行,以及其他以 0.5 结尾的行,总共有 14 行我必须删除。 我的数据框将从 98 行减少到 84 行。

我可以按日期过滤,但我没有找到太多可以帮助我的资料,

谢谢

【问题讨论】:

  • 您要保留哪些行或要删除哪些行?
  • 谢谢,我要删除的行是 c (1: 3, 7, 15:18, 22,27,41,51) 我可以手动完成,但我想知道如何处理 N 列中的这种数据模式
  • 我需要通过 reprex 学习如何做到这一点,因为我想将该逻辑扩展到互联网上的其他 html 表。谢谢
  • 您要从数据中删除的模式是什么?
  • 一个小标题:98 x 10 N President President President Term of office Term of office Title Form of entry Vice President 1 N "Presiden~ "Presiden~ President Start End Title 条目形式 "Vice President" 2 0.5 "José de ~ "José de ~ José de S~ José de San Mart~ José de San Mart ~ José~ José de San Ma~ "José de San Ma~ 3 0.5 "" "" José de S~ 3 August ..是第 N 列,赞助人是 0.5, 2.5, 在行中

标签: r regex filter rvest


【解决方案1】:

由于来自网站的数据有重复的列名,我们可以使用janitor::clean_names() 来获得干净的列名,然后只保留n 列中包含整数的行。

library(rvest)
library(dplyr)

read_html("https://en.wikipedia.org/wiki/List_of_presidents_of_Peru") %>% 
  html_nodes("table") %>% 
  .[3] %>% 
  html_table(header = NA,trim = T) %>%
  .[[1]] %>%
  janitor::clean_names() %>%
  filter(grepl('^\\d+$', n)) -> result

result

# A tibble: 85 x 10
#   n     president president_2 president_3   term_of_office  term_of_office_2 title         
#   <chr> <chr>     <chr>       <chr>         <chr>           <chr>            <chr>         
# 1 1     ""        ""          José de la R… 28 February 18… 23 June 1823     President of …
# 2 2     ""        ""          José Bernard… 16 August 1823  18 November 1823 President of …
# 3 2     ""        ""          José Bernard… 18 November 18… 10 February 1824 Constitutiona…
# 4 3     ""        ""          José de La M… 10 June 1827    7 June 1829      Constitutiona…
# 5 4     ""        ""          Agustín Gama… 7 June 1829     19 December 1829 Antonio Gutié…
# 6 4     ""        ""          Agustín Gama… 1 September 18… 19 December 1829 Provisional P…
# 7 4     ""        ""          Agustín Gama… 19 December 18… 19 December 1833 Constitutiona…
# 8 5     ""        ""          Luis José de… 21 December 18… 21 December 1833 Provisional P…
# 9 6     ""        ""          Felipe Salav… 25 February 18… 7 February 1836  Supreme Head …
#10 7     ""        ""          Agustín Gama… 20 January 183… 15 August 1839   Provisional P…
# … with 75 more rows, and 3 more variables: form_of_entry <chr>, vice_president <chr>,
#   vice_president_2 <chr>

【讨论】:

  • 谢谢,这正是我需要的。为我工作
猜你喜欢
  • 2021-03-14
  • 1970-01-01
  • 2021-07-27
  • 2017-12-05
  • 1970-01-01
  • 1970-01-01
  • 2014-07-22
  • 2015-07-07
  • 2018-01-16
相关资源
最近更新 更多