如何从 R 中导入的维基百科 table 中删除引用?
How to remove reference from imported wikipedia table in R?
我想从维基百科导入 table,但它包含括号中的参考链接。
我从
https://en.wikipedia.org/wiki/Statistics_of_the_COVID-19_pandemic_in_Poland
导入这个 table
我的代码:
library("rvest")
url <- "https://en.wikipedia.org/wiki/Statistics_of_the_COVID-19_pandemic_in_Poland"
xpathd <- "/html/body/div[3]/div[3]/div[5]/div[1]/table[2]"
dane <- url %>%
read_html() %>%
html_nodes(xpath = xpathd) %>%
html_table()
dane <- dane[[1]]
head(dane)
当我使用它时,我得到的数据 table 带有括号和参考编号:
如何删除它?
一种简单的方法是在要从中删除数字的列上使用 parse_number
。
library(dplyr)
new_data <- dane %>%
mutate(across(`Confirmed daily[c]`:`Unofficial deaths daily[f]`,
readr::parse_number))
parse_number
returns 字符串中的第一个数字。
您可以使用gsub()
删除参考零件的图案。
library(dplyr)
dane %>%
mutate(across(.fns = ~ gsub("\[.*?\]", "", .)))
我想从维基百科导入 table,但它包含括号中的参考链接。
我从
https://en.wikipedia.org/wiki/Statistics_of_the_COVID-19_pandemic_in_Poland
我的代码:
library("rvest")
url <- "https://en.wikipedia.org/wiki/Statistics_of_the_COVID-19_pandemic_in_Poland"
xpathd <- "/html/body/div[3]/div[3]/div[5]/div[1]/table[2]"
dane <- url %>%
read_html() %>%
html_nodes(xpath = xpathd) %>%
html_table()
dane <- dane[[1]]
head(dane)
当我使用它时,我得到的数据 table 带有括号和参考编号:
如何删除它?
一种简单的方法是在要从中删除数字的列上使用 parse_number
。
library(dplyr)
new_data <- dane %>%
mutate(across(`Confirmed daily[c]`:`Unofficial deaths daily[f]`,
readr::parse_number))
parse_number
returns 字符串中的第一个数字。
您可以使用gsub()
删除参考零件的图案。
library(dplyr)
dane %>%
mutate(across(.fns = ~ gsub("\[.*?\]", "", .)))