【发布时间】: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 officeTerm of officeTitleForm of entryVice President1 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, 在行中