关于这个数据提取的问题我做了。我想创建一个包含数据的条形图,但不幸的是我无法将提取的字符转换为R中的数字。如果我在文本编辑器中编辑文件,根本没有问题,但我想做整个在R.中的过程。这是代码:
install.packages("rvest")
library(rvest)
url <- "https://en.wikipedia.org/wiki/Corporate_tax"
corporatetax <- url %>%
read_html() %>%
html_nodes(xpath='//*[@id="mw-content-text"]/div/table[5]') %>%
html_table()
str(corporatetax)
因此在corporatetax
中有一个data.frame,其中包含3个变量,所有这些变量都是字符。我的问题,我还没有解决,我应该如何继续将第二列和第三列转换为数字来创建条形图?我尝试过使用sapply()和dplyr(),但没有找到正确的方法。
谢谢!
您可能会尝试像这样清理表格
library(rvest)
library(stringr)
library(dplyr)
url <- "https://en.wikipedia.org/wiki/Corporate_tax"
corporatetax <- url %>%
read_html() %>%
# your xpath defines the single table, so you can use html_node() instead of html_nodes()
html_node(xpath='//*[@id="mw-content-text"]/div/table[5]') %>%
html_table() %>% as_tibble() %>%
setNames(c("country", "corporate_tax", "combined_tax"))
corporatetax %>%
mutate(corporate_tax=as.numeric(str_replace(corporate_tax, "%", ""))/100,
combined_tax=as.numeric(str_replace(combined_tax, "%", ""))/100
)